gpt4 book ai didi

java - 使用 Java 有条件地访问同一级别的 JsonElements

转载 作者:行者123 更新时间:2023-11-30 02:09:45 29 4
gpt4 key购买 nike

我有一个大型且复杂的 JSON 对象,如下所示:

{
"FDN": [{
"OT": 1,
"CL": [{
"K": [{
"FID": 348,
"VAL": "20822548"
}, {
"FID": "LANGUAGE_ID",
"VAL": "FR"
}]
}],
"NK": null,
"SS": 0,
"ITID": "NEWS",
"TSID": 44969007,
"DT": "20180504123302006"
}, {
"OT": 1,
"CL": [{
"K": [{
"FID": 348,
"VAL": "20822549"
}, {
"FID": "LANGUAGE_ID",
"VAL": "AR"
}]
}],
"NK": null,
"SS": 0,
"ITID": "NEWS",
"TSID": 44969008,
"DT": "20180504123302007"
}, {
"OT": 1,
"CL": [{
"K": [{
"FID": 348,
"VAL": "20822550"
}, {
"FID": "LANGUAGE_ID",
"VAL": "FR"
}]
}],
"NK": null,
"SS": 0,
"ITID": "NEWS",
"TSID": 44969009,
"DT": "20180504123302007"
}, {
"OT": 1,
"CL": [{
"K": [{
"FID": 348,
"VAL": "20822551"
}, {
"FID": "LANGUAGE_ID",
"VAL": "AR"
}]
}],
"NK": null,
"SS": 0,
"ITID": "NEWS",
"TSID": 44969010,
"DT": "20180504123302008"
}, {
"OT": 1,
"CL": [{
"K": [{
"FID": 348,
"VAL": "20822552"
}, {
"FID": "LANGUAGE_ID",
"VAL": "FR"
}]
}],
"NK": null,
"SS": 0,
"ITID": "NEWS",
"TSID": 44969011,
"DT": "20180504123302009"
}, {
"OT": 1,
"CL": [{
"K": [{
"FID": 348,
"VAL": "20822553"
}, {
"FID": "LANGUAGE_ID",
"VAL": "FR"
}]
}],
"NK": null,
"SS": 0,
"ITID": "NEWS",
"TSID": 44969012,
"DT": "20180504123302010"
}, {
"OT": 1,
"CL": [{
"K": [{
"FID": 348,
"VAL": "20822554"
}, {
"FID": "LANGUAGE_ID",
"VAL": "FR"
}]
}],
"NK": null,
"SS": 0,
"ITID": "NEWS",
"TSID": 44969013,
"DT": "20180504123302010"
}, {
"OT": 1,
"CL": [{
"K": [{
"FID": 348,
"VAL": "20822573"
}, {
"FID": "LANGUAGE_ID",
"VAL": "AR"
}]
}],
"NK": null,
"SS": 0,
"ITID": "NEWS",
"TSID": 44969065,
"DT": "20180504080929368"
}, {
"OT": 0,
"CL": [{
"K": [{
"FID": 348,
"VAL": "20822573"
}, {
"FID": "LANGUAGE_ID",
"VAL": "AR"
}]
}],
"NK": null,
"SS": 0,
"ITID": "NEWS",
"TSID": 44969066,
"DT": "20180504080929369"
}, {
"OT": 1,
"CL": [{
"K": [{
"FID": 348,
"VAL": "20822574"
}, {
"FID": "LANGUAGE_ID",
"VAL": "AR"
}]
}],
"NK": null,
"SS": 0,
"ITID": "NEWS",
"TSID": 44969067,
"DT": "20180504082130104"
}]
}

由此,我只需要考虑这样的“CL”单元:

"CL": [{
"K": [{
"FID": 348,
"VAL": "20822573"
}, {
"FID": "LANGUAGE_ID",
"VAL": "AR"
}]
}],

检查LANGUAGE_ID是否为“AR”,然后将相应键“FID:348”的相关值(20822573)添加到列表中。只是一个前任:

 if(LANGUAGE_ID.equals("AR")){
IdList.add("20822573");
}

我不能这样做,因为两个键都处于同一级别。

到目前为止我的代码是:

public static void main(String[] a) throws IOException {
FileReader jsonReader = new FileReader(new File("json.json"));
Gson gson = new Gson();
Map<String,String> fidMap = new HashMap<String, String>();
JsonObject jsonObject = gson.fromJson(jsonReader, JsonObject.class);
JsonArray fdnJsonArray = (JsonArray) jsonObject.get("FDN");

fdnJsonArray.forEach(jsonElement -> {
JsonArray clJsonArray = jsonElement.getAsJsonObject().get("CL").getAsJsonArray();
clJsonArray.forEach(clJsonItem -> {
JsonArray kJsonArray = clJsonItem.getAsJsonObject().get("K").getAsJsonArray();
kJsonArray.forEach(kArrayElement -> {
String fid = kArrayElement.getAsJsonObject().get("FID").getAsString();
if(fid != null && !fid.isEmpty() && fid.equalsIgnoreCase("LANGUAGE_ID")) {
String val = kArrayElement.getAsJsonObject().get("VAL").getAsString();
System.out.println(fid + ":"+ val);
}
});
});
});
}

非常感谢您对此的帮助!

最佳答案

考虑使用jsonpath为了这。获取 CL 对象的这个特定问题的一个简单示例就像 $..CL 一样简单。然后你可以用纯java操作返回的对象。您可以在线快速制作原型(prototype) here 。要进一步细化 jsonpath 以包含语言 ID,您可以在此处查看文档:http://goessner.net/articles/JsonPath/index.html#e2

关于java - 使用 Java 有条件地访问同一级别的 JsonElements,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50434069/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com