对于以下 json,java 中的 JsonPath 是什么
what would be the JsonPath in java for the following json
我正在进行控制器测试,并且正在测试返回的 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();
}
我的问题是,如何在帐户字段中写入帐户名称的 json路径?谢谢
要访问 accountName 值,您应该使用此表达式:
$.accounts[0].accountName
你也可以用这个表达式得到所有的accountName :
$.accounts[*].accountName
希望对您有所帮助!
我正在进行控制器测试,并且正在测试返回的 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();
}
我的问题是,如何在帐户字段中写入帐户名称的 json路径?谢谢
要访问 accountName 值,您应该使用此表达式:
$.accounts[0].accountName
你也可以用这个表达式得到所有的accountName :
$.accounts[*].accountName
希望对您有所帮助!