以下json中java的JsonPath是什么

时间:2015-04-02 08:48:25

标签: java json testing jsonpath

我正在进行控制器测试并正在测试以查看返回的json值是否正确。这是我的json { "firstName": "Bob", "lastName": "Jones", "phoneNumber": "0123456789", "jobTitle": "Software Developer", "companyName": "BFS", "industry": { "name": "legal" }, "companySize": { "noOfEmployees": "1_to_10" }, "accounts": [ { "accountName": "TestAccount1", "accountNo": "123", "accountType": "PREPAY", "creditBalance": "25.00", "creditLimit": "500.00", "concurrentSubscriptionLimit": "10", "hitsPerSubscriptionItemLimit": "20", "hourlyRequestLimit": "30" } ] }

我当前的测试如下:mockMvc.perform(request) .andDo(MockMvcResultHandlers.print()) .andExpect(status().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(); }

我的问题是,如何在accounts字段中为accountName编写jsonPath?感谢。

1 个答案:

答案 0 :(得分:1)

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

$.accounts[0].accountName

您还可以使用以下表达式获取所有accountName:

$.accounts[*].accountName

希望它有所帮助!