我有一个庞大而复杂的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",然后添加相应密钥的相关值(20822573)" FID:348"到列表。 只是一个例子:
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);
}
});
});
});
}
非常感谢您的帮助!