jsonpath:JSON路径:$ .id无值,例外:路径'id'正在应用于数组.数组不能具有属性 [英] jsonpath: No value for JSON path: $.id, exception: Path 'id' is being applied to an array. Arrays can not have attributes

查看:345
本文介绍了jsonpath:JSON路径:$ .id无值,例外:路径'id'正在应用于数组.数组不能具有属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用jsonPath读取json的内容,但出现错误.

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

这里是junit测试方法:

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","....}, ....}]

我收到此错误:

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

有人可以帮助我吗?

谢谢

推荐答案

响应返回一个JSON数组,并使用"$.id"尝试访问该数组的id属性.正如错误消息告诉您的那样,这是不可能的.

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.

相反,在数组的第一个元素上测试idname属性:

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")))

这篇关于jsonpath:JSON路径:$ .id无值,例外:路径'id'正在应用于数组.数组不能具有属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆