问题描述
我有一个 JSON 响应,我想从中解析和提取数据.这是 JSON 响应
[{"od_pair":"7015400:8727100",桶":[{"桶":"C00",原始":2,可用":2},{"桶":"A01",原始":76,可用":0},{"桶":"B01","原文":672,可用":480}]},{"od_pair":"7015400:8814001",桶":[{"桶":"C00",原始":2,可用":2},{"桶":"A01",原始":40,可用":40},{"桶":"B01","原文":672,可用":672},{"桶":"B03","原文":632,可用":632},{"桶":"B05",原始":558,可用":558}]}]我想提取每个 od_pair 和桶的值,并在其中可用.
@Fenio 在 访问带有嵌套对象的 jsonpath 元素 中的解决方案是最好的方法.我重构的代码片段如下所示:
List>LegList = jsonPath.getList("$");for (HashMap singleLeg : LegList) {String OD_pair = (String) singleLeg.get("od_pair");//List>bucketsList = jsonPath.param("j", j).getList("[j].buckets");List>bucketsList = jsonPath.getList("singleLeg.buckets");for (HashMap singleBucket : bucketsList) {String BucketCode = (String) singleBucket.get("bucket");可用字符串 =(字符串)singleBucket.get("可用");
我想验证我提取的 bucketsList 是否正确.早些时候我使用了一个带有参数 j 的 for 循环.但是使用这种更干净、更好的方法,我想知道我提取bucketsList的方式是否正确
我设法解决了这个问题.我明白我哪里出错了.替换
List>bucketsList = jsonPath.getList("singleLeg.buckets");
有了这个
List>bucketsList = (List) singleLeg.get("buckets");
已解决我的问题,现在一切按预期进行.
因为我已经在 singleLeg 循环中,所以我需要调用的是循环中的 buckets 对象,而不是尝试从 rootpath 访问 buckets.
向@Fenio 大喊大叫,他在访问 jsonpath 中建议了最佳方法带有嵌套对象的元素
I have a JSON response which I want to parse and extract the data from. Here is the JSON response
[
{
"od_pair":"7015400:8727100",
"buckets":[
{
"bucket":"C00",
"original":2,
"available":2
},
{
"bucket":"A01",
"original":76,
"available":0
},
{
"bucket":"B01",
"original":672,
"available":480
}
]
},
{
"od_pair":"7015400:8814001",
"buckets":[
{
"bucket":"C00",
"original":2,
"available":2
},
{
"bucket":"A01",
"original":40,
"available":40
},
{
"bucket":"B01",
"original":672,
"available":672
},
{
"bucket":"B03",
"original":632,
"available":632
},
{
"bucket":"B05",
"original":558,
"available":558
}
]
}
]
I want to extract each od_pair and the values of of bucket and available within them.
@Fenio's solution in Accessing jsonpath elements with nested objects has the best approaches. The code snippet that I have refactored looks like this:
List<HashMap<String, Object>> LegList = jsonPath.getList("$");
for (HashMap<String, Object> singleLeg : LegList) {
String OD_pair = (String) singleLeg.get("od_pair");
//List<HashMap<String, Object>> bucketsList = jsonPath.param("j", j).getList("[j].buckets");
List<HashMap<String, Object>> bucketsList = jsonPath.getList("singleLeg.buckets");
for (HashMap<String, Object> singleBucket : bucketsList) {
String BucketCode = (String) singleBucket.get("bucket");
String Available = (String)
singleBucket.get("available");
I want to verify if the bucketsList that I am extracting is correct. Earlier I used a for loop with the parameter j. But with this approach which is lot more cleaner and nicer, I wish to understand if I am right in the way am extracting the bucketsList
I managed to resolve this. I understood where I was going wrong. Replacing
List<HashMap<String, Object>> bucketsList = jsonPath.getList("singleLeg.buckets");
with this
List<HashMap<String, Object>> bucketsList = (List<HashMap<String, Object>>) singleLeg.get("buckets");
Has resolved my issue and now things work as expected.
Since I was already within singleLeg loop, all I needed to call was the buckets object within the loop rather than trying to access the buckets from the rootpath.
Big shoutout to @Fenio who advised the best approaches in Accessing jsonpath elements with nested objects
这篇关于使用 hashmap List 访问嵌套循环中的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!