java - 以下 json 的 java 中的 JsonPath 是什么

标签 java json testing jsonpath

我正在进行 Controller 测试,并且正在测试返回的 json 值是否正确。这是我的 json{ "firstName": "鲍勃", "lastName": "琼斯", “电话号码”:“0123456789”, "jobTitle": "软件开发人员", "公司名称": "BFS"​​, “行业”: { “名称”:“合法” }, “公司规模”: { “noOfEmployees”:“1_to_10” }, “账户”:[ { "accountName": "TestAccount1", "账号": "123", "accountType": "预付", “信用余额”:“25.00”, "creditLimit": "500.00", “并发订阅限制”:“10”, "hitsPerSubscriptionItemLimit": "20", "hourlyRequestLimit": "30" } ]

我当前的测试如下所示:mockMvc.perform(request) .andDo(MockMvcResultHandlers.print()) .andExpect(状态().isCreated()) .andExpect(jsonPath("$.firstName", is("Bob"))) .andExpect(jsonPath("$.lastName", is("Jones"))) .andExpect(jsonPath("$.phoneNumber", is("0123456789"))) .andExpect(jsonPath("$.jobTitle", is("Software Developer"))) .andExpect(jsonPath("$.companyName", is("BFS"​​))) .andExpect(jsonPath("$.industry.name", is("legal"))) .andExpect(jsonPath("$.companySize.noOfEmployees", is("1_to_10"))) .andReturn(); }

我的问题是,如何在帐户字段中为帐户名编写 jsonPath?谢谢。

最佳答案

要访问 accountName 值,您应该使用此表达式:

$.accounts[0].accountName

您还可以使用此表达式获取所有帐户名:

$.accounts[*].accountName

希望对您有所帮助!

关于java - 以下 json 的 java 中的 JsonPath 是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29408709/

相关文章:

java.awt -- 当java输出图像到我的显示器(屏幕)时,输出到显示器卡的文件在哪里?

java - 使用 WireMock 模拟 Http 服务器响应。 FileNotFoundException异常

php - PHP 中的 MongoDB - 如何将项目插入集合中的数组?

ruby-on-rails - 如何加速 Rails 5 "system"测试?

javascript - import 在测试时返回 undefined 而不是 react 组件

testing - 在 hudson 中设置测试构建作业,检测 make 何时无法编译

java - JDK中的先进先出队列

java - Spring Boot应用程序,我想用application.properties配置index.html的位置,但不起作用

java - 如何获取/设置 JdbcRealm 的盐

json - 从 Node.js 中的 TCP 套接字读取字符串时出现问题