【发布时间】:2018-07-12 00:11:58
【问题描述】:
我有一个响应的端点
{
"fruits" : {
"apple" : "green",
"banana" : 99,
"oranges" : "floridaBreed",
"lemons" : "sicilian",
"grapes" : "red",
"cherries" : 12,
"guava" : "sour",
"tomato" : 13,
"melons" : "yellow",
"pomegranate" : 37,
},
"isRaw" : null,
"foodtype" : {
"typeOne" : "non-veg",
"typeTwo" : "veg"
},
"isCooked" : [ ],
"isReady" : [ ],
"isExpired" : [ ],
"isHealthy" : null,
"serialnumber" : 5555,
"isAvailable" : "Yes",
"dietValue" : {
"nutrition" : [ {
"vitamin" : "yes",
"vitaminValue" : 3
}, {
"calcium" : "no",
"calciumValue" : 0
} ]
},
"isEdible" : null
}
我有一些代码可以得到这个,有点我无法得到嵌套的值对象。例如,我可以获得'isAvailable' 的值,即='yes' 但是如果我想获得'grapes' 或'calcium' 的值,那么它不起作用。有什么想法吗?
public String sendGetMessageValue(String messageId) {
Map<String, String> response = doInternalModuleApiGet(messageId, 200);
return response.get("fruits.grapes");
}
public Map<String, String> doInternalModuleApiGet(
String messageId,
int expectedStatus) {
String url = getInternalUrl() + "/" + messageId;
return sendHttpGetRequestAndGetResponse(url, expectedStatus).as(Map.class);
}
private ResponseBodyExtractionOptions sendHttpGetRequestAndGetResponse(String url, int expectedStatus) {
return given()
.header("Authorization", "basic " + B64Code.encode("dummyuser:dummypass"))
.get(url)
.then()
.log().ifError().and()
.assertThat().statusCode(equalTo(expectedStatus)).and().extract().body();
}
public String getInternalUrl() {
return ("http://127.0.0.1:8080/api");
}
【问题讨论】:
-
为什么你不让杰克逊反序列化 JSON
-
我不知道,可以。可以举个例子吗
-
或使用@Rohan Kadu 解决方案
标签: java json http-get rest-assured