Java jsonpath:JSON路径没有值:$.id,异常:path';id';正在应用于数组。数组不能有属性
我试图用jsonPath读取json的内容,但得到了一个错误 以下是junit测试方法:Java jsonpath:JSON路径没有值:$.id,异常:path';id';正在应用于数组。数组不能有属性,java,json,spring,mocking,mockmvc,Java,Json,Spring,Mocking,Mockmvc,我试图用jsonPath读取json的内容,但得到了一个错误 以下是junit测试方法: mockMvc.perform(get("/path") .andExpect(status().isOk()) .andExpect(jsonPath("$.id", is(1))) .andExpect(jsonPath("$.name", is("NAME"))) .andR
mockMvc.perform(get("/path")
.andExpect(status().isOk())
.andExpect(jsonPath("$.id", is(1)))
.andExpect(jsonPath("$.name", is("NAME")))
.andReturn().getResponse().getContentAsString();
以下是请求返回给我的内容:
[{"id":1,"name":"NAME","....}, ....}]
我得到了这个错误:
No value for JSON path: $.id, exception: Path 'id' is being applied to an array. Arrays can not have attributes.
有人能帮我吗
感谢响应返回一个JSON数组,并使用
“$.id”
尝试访问该数组的id
属性。正如错误消息告诉您的那样,这是不可能的
改为在数组的第一个元素上测试id
和name
属性:
.andExpect(jsonPath("$[0].id", is(1)))
.andExpect(jsonPath("$[0].name", is("NAME")))