JSON 示例
[
{
"PROPERTY1": [
{
"name": "String"
"value: 0
},
{
"surname": "String",
"age": 20
}
]
},
{
"PROPERTY2": [
{
"name": "String"
"value: 1
},
{
"surname": "String",
"age": 22
}
]
}
]
我想要一个数组[PROPERTY1,PROPERTY2]
如何使用最可靠的JSON路径提取器?
.extract().path("collect{it.value}"); //it's not working
下面是一个很长的机会,但这是我现在能想到的唯一解决方案
代码很容易解释,否则请告诉我,我会添加更多评论
String payload = "[{rn" + " "PROPERTY1": [{rn" + " "name": "String",rn"
+ " "value": 0rn" + " },rn" + " {rn"
+ " "surname": "String",rn" + " "age": 20rn" + " }rn"
+ " ]rn" + " },rn" + " {rn" + " "PROPERTY2": [{rn"
+ " "name": "String",rn" + " "value": 1rn" + " },rn"
+ " {rn" + " "surname": "String",rn" + " "age": 22rn"
+ " }rn" + " ]rn" + " }rn" + "]";
JSONArray array = new JSONArray(payload);
Iterator<Object> iterator = array.iterator();
ArrayList<String> keys = new ArrayList();
while (iterator.hasNext()) {
JSONObject jsonObject = (JSONObject) iterator.next();
keys.addAll(jsonObject.keySet());
}
System.out.println(keys);
确保将JSON负载提取为字符串
上述的响应将是[PROPERTY1, PROPERTY2]