简体   繁体   English

Google Maps中的JSONObject用Java解析

[英]JSONObject from Google Maps parse in Java

I'm trying to parse Java Object to get data from a URL. 我正在尝试解析Java对象以从URL获取数据。 I'm using getJSONObject and getString methods from org.json.JSONObject library but this it's not working. 我使用getJSONObjectgetString从方法org.json.JSONObject库,但这个它不工作。 I'm doing something like... 我正在做类似...

JSONObject jsonCoord = json.getJSONObject("results")
                .getJSONObject("geometry")
                .getJSONObject("location");

coordinates[0] = jsonCoord.getString("lat");
coordinates[1] = jsonCoord.getString("lng");

JSON document I want to parse (Part 1) 我要解析的JSON文档(第1部分)

JSON document I want to parse (Part 2) 我要解析的JSON文档(第2部分)

How can I get "lat" and "lng" which is inside of "geometry"? 如何获得“几何体”内部的“ lat”和“ lng”?

Here it is: 这里是:

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

    String content = new String(Files.readAllBytes(Paths.get("test.json")), "UTF-8");
    JSONObject json = new JSONObject(content);
    JSONArray results = json.getJSONArray("results");
    JSONObject result = (JSONObject) results.get(0); //or iterate if you need for each
    JSONObject jsonCoord = result.getJSONObject("geometry").getJSONObject("location");
    System.out.println(jsonCoord);

    String[] coordinates = new String[2];
    coordinates [0] = jsonCoord.getString("lat");
    coordinates[1] = jsonCoord.getString("lng");

    System.out.println(Arrays.asList(coordinates));

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM