如何访问JSON值?

时间:2018-03-26 18:04:01

标签: java json

对于JSON来说很新,这可能非常简单,但我不知道如何访问此JSON中的“坐标”我知道如何从resourceSets转到资源但却陷入“点”:

{
   "resourceSets":[
      {
         "estimatedTotal":1,
         "resources":[
            {
               "__type":"Location:http:\/\/schemas.microsoft.com\/search\/local\/ws\/rest\/v1",
               "bbox":[
                  51.3223903,
                  -0.2634519,
                  51.3246386,
                  -0.2598541
               ],
               "name":"name",
               "point":{
                  "type":"Point",
                  "coordinates":[
                     51.3235145,
                     -0.261653
                  ]
               }
            }
        ]
      }
   ]
}

到目前为止我的代码:

JSONParser parser = new JSONParser();
                JSONObject jObj = (JSONObject)parser.parse(result);
                JSONArray jsonArray = (JSONArray)jObj.get("resourceSets");

                for (int i = 0; i < jsonArray.size(); i ++) {
                    JSONObject jObjResourceSets = (JSONObject)jsonArray.get(i);
                    JSONArray jsonArray2 = (JSONArray)jObjResourceSets.get("resources");
                    System.out.println("Coords" + jObjResourceSets.get("point"));


                }

3 个答案:

答案 0 :(得分:3)

通常,此代码适用于给定的JSON对象。但是我会将经过测试的格式化JSON值放在java代码下面,这样你就可以测试它了。

注意,此代码将为您提供对象中所有元素的所有坐标。

public static void main(String[] args) throws Exception {

    String result = getJsonString();
    // Getting root object
    JSONParser parser = new JSONParser();
    JSONObject jObj = (JSONObject)parser.parse(result);
    // Getting resourceSets Array
    JSONArray jsonArray = (JSONArray)jObj.get("resourceSets");

    for (int i = 0; i < jsonArray.size(); i++) {
        // Getting the i object of resourceSet
        JSONObject jObjResourceSets = (JSONObject) jsonArray.get(i);
        // Getting resources list of the current element
        JSONArray jsonArray2 = (JSONArray) jObjResourceSets.get("resources");
        for (int j = 0; j < jsonArray2.size(); j++) {
            // Getting the j resource of the resources array
            JSONObject resource = (JSONObject) jsonArray2.get(j);
            // Getting the point object of the current resource
            JSONObject point = (JSONObject) resource.get("point");
            // Getting the coordinates list of the point
            JSONArray coordinates = (JSONArray) point.get("coordinates");
            for (int k = 0; k < coordinates.size(); k++) {
                System.out.println("Coordinates[" + k + "]: " + coordinates.get(k));
            }
            // Printing an empty line between each object's coordinates
            System.out.println();
        }
    }
}

经过测试的JSON对象:

{
   "resourceSets":[
      {
         "estimatedTotal":1,
         "resources":[
            {
               "__type":"Location:http:\/\/schemas.microsoft.com\/search\/local\/ws\/rest\/v1",
               "bbox":[
                  51.3223903,
                  -0.2634519,
                  51.3246386,
                  -0.2598541
               ],
               "name":"name",
               "point":{
                  "type":"Point",
                  "coordinates":[
                     51.3235145,
                     -0.261653
                  ]
               }
            }
        ]
      }
   ]
}

如果有效,请将其标记为答案。

祝你好运^^

答案 1 :(得分:3)

让我们分析你正在做什么(并且需要做什么),一步一步,以获得&#34;坐标&#34;。

首先,JSON是一种传输静态数据的好语言。它就像一本字典,你有一把钥匙和各自的价值。键应始终为String,但值可以是String,int / double或甚至是其他JSON对象的数组。这就是你拥有的。

例如,&#34;估计总数&#34;是来自&#34; resourceSet&#34;的元素(JSONObject)。数组(JSONArray)。

JSONArray jsonArray = (JSONArray)jObj.get("resourceSets");

你在这里说的很简单:从你的整体JSONObject - jObj - 你想用密钥&#34; resourceSets&#34;提取数组。

现在您可以直接访问&#34; resourceSets&#34;数组元素:&#34;估计总数&#34;,&#34;资源&#34;等等。因此,通过应用相同的逻辑,以便访问&#34;坐标&#34;我们需要访问&#34;资源&#34;阵列。我的意思是像以前一样创建一个JSONArray对象。

JSONArray jsonResourcesArray = (JSONArray)jObjResourceSets.get("resources");

我希望明白这里jsonResourcesArray的内容是什么。它是&#34; __ type&#34;,&#34; bbox&#34;,&#34; name&#34;,&#34; point&#34;,(...)的JSON数组。协调器内部如何&#34;点&#34; JSON对象。我们如何访问它?

JSONObject jsonPointObject = (JSONObject) jsonResourcesArray.get("point");

你知道&#34; jsonPointObject&#34;将以下JSON对象作为其值:&#34; type&#34;和&#34;坐标&#34;。 Coordinates是一个值数组,所以我们必须使用JSONArray或JSONObject吗?

JSONArray jsonCoordinatesArray = (JSONArray) jsonPointObject.get("coordinates");

我们的意思是:从jsonPointObject我们想要提取具有键&#34;坐标&#34;的数组。现在,您的数组是一个值为jsonCoordinatesArray.get(0)jsonCoordinatesArray.get(0)的JSONArray。

现在你不仅要获得这些值的代码,还要了解JSON的工作原理以及Java如何与之交互,以便从现在开始解决任何Json问题。

答案 2 :(得分:0)

您需要以下代码才能获取数据。

JSONArray jsonArray2 = (JSONArray)jObjResourceSets.get("resources");
        /**
         * here we should note that the "resources" is only having one JSON object hence we can take it as below
         * from 0th index using ((JSONObject)jsonArray2.get(0)) these piece of code and the next part is to take the point JSONObject 
         * from the object.
         */
        JSONObject temp = (JSONObject)((JSONObject)jsonArray2.get(0)).get("point"); 
        System.out.println("Coords"+temp.get("coordinates")); //this will give you the coordinates array

        //now if you want to do further processing, traverse in the jsonArray like below
        JSONArray arr= (JSONArray)temp.get("coordinates");
        System.out.println("X coordinate:"+arr.get(0));
        System.out.println("Y coordinate:"+arr.get(1)); 

有关JSONObject和JSONArray的更多信息和更多详细信息,您可以浏览此链接
parsing-jsonarray-and-jsonobject-in-java-using-json-simple-jar

json-simple-example-read-and-write-json