jsonpath: No value for JSON path: $.id, exception: Path 'id' is being applied to an array. Arrays can not have attributes

烈酒焚心 提交于 2020-01-03 17:28:09

问题


i tried to read the content of the json with jsonPath and i get an error.

Here the junit test method:

mockMvc.perform(get("/path")
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.id", is(1)))
                .andExpect(jsonPath("$.name", is("NAME")))
                .andReturn().getResponse().getContentAsString();

here is what the request return me:

[{"id":1,"name":"NAME","....}, ....}]

i got this error:

No value for JSON path: $.id, exception: Path 'id' is being applied to an array. Arrays can not have attributes.

can someone helps me.

Thanks


回答1:


The response returns a JSON array and using "$.id" you try to access the id property of that array. This - as the error message tells you - is not possible.

Test the id and name property on the first element of the array instead:

.andExpect(jsonPath("$[0].id", is(1)))
.andExpect(jsonPath("$[0].name", is("NAME")))


来源:https://stackoverflow.com/questions/35251927/jsonpath-no-value-for-json-path-id-exception-path-id-is-being-applied-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!