英文:
Want to read JSONArray with JSONObject, but got the error A non-empty JSON Pointer
问题
[
{
"datasetid": "country-flags",
"recordid": "d661d0a8676bf4d7563114c1d9c465987df22132",
"fields": {
"num_un": 32,
"geolocation": [
-38.416097,
-63.616672
],
"dialing_code": "54",
"a3_un": "ARG",
"country": "Argentina",
"flag": {
"mimetype": "image/png",
"format": "PNG",
"filename": "ar.png",
"width": 16,
"id": "fceb4235ce95c8597bfa77d0db0181a0",
"height": 11,
"thumbnail": true
},
"a2_iso": "AR"
},
"geometry": {
"type": "Point",
"coordinates": [
-63.616672,
-38.416097
]
},
"record_timestamp": "2016-09-26T07:48:38.162+02:00"
},
more...
]
JsonReader jsonReader = Json.createReader(new FileReader(getClass().getResource("country-flags.json").getPath()));
JsonArray arr = jsonReader.readArray();
for (int i = 0; i < arr.size(); i++) {
JsonObject obj = arr.getJsonObject(i);
JsonArray coordinates = obj.getJsonObject("geometry").getJsonArray("coordinates");
double longitude = coordinates.getJsonNumber(0).doubleValue();
double latitude = coordinates.getJsonNumber(1).doubleValue();
System.out.println("Longitude: " + longitude);
System.out.println("Latitude: " + latitude);
}
Error message: javax.json.JsonException: A non-empty JSON Pointer must begin with a '/'
Can someone help me out?!
英文:
My Json FILE (it´s an array! )
> [
{
"datasetid": "country-flags",
"recordid": "d661d0a8676bf4d7563114c1d9c465987df22132",
"fields": {
"num_un": 32,
"geolocation": [
-38.416097,
-63.616672
],
"dialing_code": "54",
"a3_un": "ARG",
"country": "Argentina",
"flag": {
"mimetype": "image/png",
"format": "PNG",
"filename": "ar.png",
"width": 16,
"id": "fceb4235ce95c8597bfa77d0db0181a0",
"height": 11,
"thumbnail": true
},
"a2_iso": "AR"
},
"geometry": {
"type": "Point",
"coordinates": [
-63.616672,
-38.416097
]
},
"record_timestamp": "2016-09-26T07:48:38.162+02:00"
},
more...
> ]
So i want to get the value from coordinates. So for this i tried to work with this:
JsonReader jsonReader = Json
.createReader(new FileReader(getClass().getResource("country-flags.json").getPath()));
JsonArray arr = jsonReader.readArray();
for(int i = 1; i<arr.size();i++)
{
JsonObject obj = arr.getJsonObject(i);
System.out.println("coordinates: " + obj.containsKey("\"coordinates\""));
System.out.println("##########");
System.out.println(obj.getValue("\"coordinates\""));
}
But i got the error:
> javax.json.JsonException: A non-empty JSON Pointer must begin with a
> '/'
Can someone help me out ?!
答案1
得分: 2
你的代码 obj.containsKey("\"coordinates\"")
将会返回 false,因为 coordinates 不是顶层键,而是第二层(嵌套)键。如果你打印 obj.keySet()
,你将会得到 [datasetid, recordid, fields, geometry, record_timestamp](仅为第一层 / 顶层键)。
如果你的 JSON 结构是固定的,你可以使用以下代码:
for (int i = 1; i < arr.size(); i++) {
JsonObject obj = arr.getJsonObject(i);
JsonObject jsonChildObject = obj.getJsonObject("geometry");
if (jsonChildObject.containsKey("coordinates"))
System.out.println(jsonChildObject.getValue("/coordinates"));
}
注意在 getValue 方法的 coordinates 参数前面有 /
。我认为这可能是你最初遇到问题的原因。
英文:
Your code obj.containsKey("\"coordinates\"")
will return false as coordinates is NOT a top level key, but is a 2nd level (nested) key. If you print obj.keySet()
, you will get [datasetid, recordid, fields, geometry, record_timestamp] (first / top level keys only).
If the structure of your JSON is fixed, you can use the following code:
for(int i = 1; i<arr.size();i++)
{
JsonObject obj = arr.getJsonObject(i);
JsonObject jsonChildObject = obj.getJsonObject("geometry");
if(jsonChildObject.containsKey("coordinates"))
System.out.println(jsonChildObject.getValue("/coordinates"));
}
Notice the /
in front of the getValue method's coordinates param. I think that was the reason you were here in the first place.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论