【发布时间】:2020-07-03 08:51:46
【问题描述】:
我有一个 JSON 文件,它是一个带有一些 JSON 对象的 JSON ARRAY,如果经度或纬度的值为空字符串“”,我想删除整个 json 对象。
我正在使用库 org.json.simple。这是我的 json 文件:
[ { "Longitude": 9.96233,
"Latitude": 49.80404 },
{
"Longitude": 6.11499,
"Latitude": 50.76891
},
{ "Longitude": 6.80592,
"Latitude": 51.53548
},
{
"Longitude": 9.50523,
"Latitude": 51.31991 },
{
"Longitude": ""
"Latitude": ""
},
{
"Longitude": 9.93368,
"Latitude": ""
},
{
"Longitude": 11.56122,
"Latitude": 48.14496
},
{
"Longitude": 13.34253,
"Latitude": 52.5319
},
{
"Longitude": 6.11327,
"Latitude": 50.77715
},
{
"Longitude": ""
"Latitude": ""
}
]
这就是我卡住的地方。 :
JSONParser jsonParser = new JSONParser();
try (FileReader reader = new FileReader ("output.json")) {
Object obj = jsonParser.parse(reader);
JSONArray list = (JSONArray) obj;
list.forEach(node -> {
String vari = (String)((JSONObject)node).get("longitude").toString();
if (vari==null) {
((JSONObject) node).remove();
System.out.println("deleted");
}
}
...
任何建议如何更改我的代码?
【问题讨论】:
-
根据您的 JSON 文件,该结构不是对象数组。所有对象都包装在一个对象中,并且该对象被添加到数组中。因此,实际上您的列表中只有一个元素。请使用 json editor 验证您的 JSON。