我如何从这个JSON中获取Coords数组的值?

时间:2017-05-10 10:31:01

标签: java json

{
"type":
    "coll",
         "locations":[
                 {"geometry":
                         {"Coords":
                                 [54.7,46.7]
                         }
                 },
                 {"geometry":
                         {"Coords":
                                 [54.7,46.7]
                         }
                 },
                 {"geometry":
                         {"Coords":
                                 [54.7,46.6]
                         }
                 },
                 {"geometry":
                         {"Coords":
                                 [54.64999833333333,46.6]
                         }
                 }
         ]
}

1 个答案:

答案 0 :(得分:0)

在使用json.simple的java中,这里的代码如下:   这里我假设json数据存在于file.json

  Object obj = parser.parse(new FileReader( "file.json" ));
  JSONObject jsonObject = (JSONObject) obj;
   JSONArray loc= (JSONArray) jsonObject.get("locations");
    Iterator i = loc.iterator();


     while (i.hasNext()) {
    JSONObject geo   = (JSONObject) (i.next());

    JSONObject coords   = (JSONObject)geo.get("geometry");
    String coord= (String)coords.get("Coords");
        System.out.println(coord);
    }