簡體   English   中英

Google Maps中的JSONObject用Java解析

[英]JSONObject from Google Maps parse in Java

我正在嘗試解析Java對象以從URL獲取數據。 我使用getJSONObjectgetString從方法org.json.JSONObject庫,但這個它不工作。 我正在做類似...

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

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

我要解析的JSON文檔(第1部分)

我要解析的JSON文檔(第2部分)

如何獲得“幾何體”內部的“ lat”和“ lng”?

這里是:

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