想要使用 JSONObject 读取 JSONArray,但是遇到了错误:A non-empty JSON Pointer。

huangapple go评论72阅读模式
英文:

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! )

> [

{
	&quot;datasetid&quot;: &quot;country-flags&quot;,
	&quot;recordid&quot;: &quot;d661d0a8676bf4d7563114c1d9c465987df22132&quot;,
	&quot;fields&quot;: {
		&quot;num_un&quot;: 32,
		&quot;geolocation&quot;: [
			-38.416097,
			-63.616672
		],
		&quot;dialing_code&quot;: &quot;54&quot;,
		&quot;a3_un&quot;: &quot;ARG&quot;,
		&quot;country&quot;: &quot;Argentina&quot;,
		&quot;flag&quot;: {
			&quot;mimetype&quot;: &quot;image/png&quot;,
			&quot;format&quot;: &quot;PNG&quot;,
			&quot;filename&quot;: &quot;ar.png&quot;,
			&quot;width&quot;: 16,
			&quot;id&quot;: &quot;fceb4235ce95c8597bfa77d0db0181a0&quot;,
			&quot;height&quot;: 11,
			&quot;thumbnail&quot;: true
		},
		&quot;a2_iso&quot;: &quot;AR&quot;
	},
	&quot;geometry&quot;: {
		&quot;type&quot;: &quot;Point&quot;,
		&quot;coordinates&quot;: [
			-63.616672,
			-38.416097
		]
	},
	&quot;record_timestamp&quot;: &quot;2016-09-26T07:48:38.162+02:00&quot;
},
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(&quot;country-flags.json&quot;).getPath()));
		JsonArray arr = jsonReader.readArray();
		
		for(int i = 1; i&lt;arr.size();i++)
		{
			 JsonObject obj = arr.getJsonObject(i);
			 System.out.println(&quot;coordinates: &quot; + obj.containsKey(&quot;\&quot;coordinates\&quot;&quot;));
			 
			 System.out.println(&quot;##########&quot;);
			 System.out.println(obj.getValue(&quot;\&quot;coordinates\&quot;&quot;));
		
			 
		}

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(&quot;\&quot;coordinates\&quot;&quot;) 将会返回 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(&quot;geometry&quot;);
    if (jsonChildObject.containsKey(&quot;coordinates&quot;))
        System.out.println(jsonChildObject.getValue(&quot;/coordinates&quot;));
}

注意在 getValue 方法的 coordinates 参数前面有 /。我认为这可能是你最初遇到问题的原因。

英文:

Your code obj.containsKey(&quot;\&quot;coordinates\&quot;&quot;) 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&lt;arr.size();i++)
{
     JsonObject obj = arr.getJsonObject(i);
     JsonObject jsonChildObject = obj.getJsonObject(&quot;geometry&quot;);
     if(jsonChildObject.containsKey(&quot;coordinates&quot;))
          System.out.println(jsonChildObject.getValue(&quot;/coordinates&quot;));
}

Notice the / in front of the getValue method's coordinates param. I think that was the reason you were here in the first place.

huangapple
  • 本文由 发表于 2020年4月9日 02:22:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/61107454.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定