如果节点不存在键,则需要将 json 键值检索为 null
Need to retrieve the json key value as null if key not present at the node
{
"a": {
"b": 1,
"c": 0
},
"values": [
{
"d": "WERTY",
"e": "details",
"f": [
{
"addressId": "vvvv",
"address": "ffff"
}
]
},
{
"d": "ZXCVB",
"e": "details"
},
{
"d": "ASDFG",
"e": "details",
"f": [
{
"addressId": "vvvv",
"address": "xxxx"
}
]
}
]
}
得到放心的响应后,我尝试使用 JsonPath 获取特定键的值。
我正在使用:
responseBody.jsonPath().getList("values.f.address)
这是返回列表 - ["ffff","xxxx"]
我想得到 - ["ffff",null,"xxxx"]
空手道可以做到吗?
它不会 return 你 null
缺席的项目,因为字段 address
实际上不存在于响应正文中。
您可以检查 f
键是否在 values
数组的每个对象中可用;
如果可用 -> 将 f
数组中每个对象中 address
的值添加到字符串列表
如果不可用 -> 添加 null
到同一个字符串列表。
我从 io.restassured.response.Response
创建了一个 org.json.JSONObject
。
Response response = given()
.when()
.get(url)
.then()
.extract()
.response();
List<String> addressList = new ArrayList<>();
JSONObject responseObject = new org.json.JSONObject(response.body().asString());
JSONArray jsonArray = responseObject.getJSONArray("values");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
if (jsonObject.keySet().contains("f")) {
JSONArray fObjectArray = jsonObject.getJSONArray("f");
for (int j = 0; j < fObjectArray.length(); j++) {
addressList.add(fObjectArray.getJSONObject(j).get("address").toString());
}
} else {
addressList.add(null);
}
}
System.out.println(addressList.toString());
这将打印以下结果;
[ffff, null, xxxx]
JsonPath = values[0].f[0].address
如果您正在使用 validatableResonse
那么您可以使用:
String res_str = Response.extract().jsonPath().getString("values[0].f[0].address");
响应应为 ValidatableResponse
类型。
{
"a": {
"b": 1,
"c": 0
},
"values": [
{
"d": "WERTY",
"e": "details",
"f": [
{
"addressId": "vvvv",
"address": "ffff"
}
]
},
{
"d": "ZXCVB",
"e": "details"
},
{
"d": "ASDFG",
"e": "details",
"f": [
{
"addressId": "vvvv",
"address": "xxxx"
}
]
}
]
}
得到放心的响应后,我尝试使用 JsonPath 获取特定键的值。 我正在使用:
responseBody.jsonPath().getList("values.f.address)
这是返回列表 - ["ffff","xxxx"] 我想得到 - ["ffff",null,"xxxx"]
空手道可以做到吗?
它不会 return 你 null
缺席的项目,因为字段 address
实际上不存在于响应正文中。
您可以检查 f
键是否在 values
数组的每个对象中可用;
如果可用 -> 将 f
数组中每个对象中 address
的值添加到字符串列表
如果不可用 -> 添加 null
到同一个字符串列表。
我从 io.restassured.response.Response
创建了一个 org.json.JSONObject
。
Response response = given()
.when()
.get(url)
.then()
.extract()
.response();
List<String> addressList = new ArrayList<>();
JSONObject responseObject = new org.json.JSONObject(response.body().asString());
JSONArray jsonArray = responseObject.getJSONArray("values");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
if (jsonObject.keySet().contains("f")) {
JSONArray fObjectArray = jsonObject.getJSONArray("f");
for (int j = 0; j < fObjectArray.length(); j++) {
addressList.add(fObjectArray.getJSONObject(j).get("address").toString());
}
} else {
addressList.add(null);
}
}
System.out.println(addressList.toString());
这将打印以下结果;
[ffff, null, xxxx]
JsonPath = values[0].f[0].address
如果您正在使用 validatableResonse
那么您可以使用:
String res_str = Response.extract().jsonPath().getString("values[0].f[0].address");
响应应为 ValidatableResponse
类型。