我正在进行 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/