Android之解析Android Map地圖返回的Json資料

來源:互聯網
上載者:User

先上一下需要解析的Json資料:

{    "results" : [       {          "address_components" : [             {                "long_name" : "荔灣區",                "short_name" : "荔灣區",                "types" : [ "sublocality", "political" ]             },             {                "long_name" : "廣州",                "short_name" : "廣州",                "types" : [ "locality", "political" ]             },             {                "long_name" : "廣東省",                "short_name" : "廣東省",                "types" : [ "administrative_area_level_1", "political" ]             },             {                "long_name" : "中國",                "short_name" : "CN",                "types" : [ "country", "political" ]             }          ],          "formatted_address" : "中國廣東省廣州荔灣區",          "geometry" : {             "bounds" : {                "northeast" : {                   "lat" : 23.15961330,                   "lng" : 113.2768870                },                "southwest" : {                   "lat" : 23.04186250,                   "lng" : 113.17712080                }             },             "location" : {                "lat" : 23.1259510,                "lng" : 113.2442380             },             "location_type" : "APPROXIMATE",             "viewport" : {                "northeast" : {                   "lat" : 23.16699020,                   "lng" : 113.30826770                },                "southwest" : {                   "lat" : 23.08489920,                   "lng" : 113.18020830                }             }          },          "types" : [ "sublocality", "political" ]       }    ],    "status" : "OK"

 

上關鍵代碼: 這隻是解析了部分,但是其他部分都一樣,不要忘記匯入gson-2.1.jar包到程式中

public void parserJson(String str){
        try {
            JSONObject jsonObject = new JSONObject(str);
            //解析了最簡單的status
            //String s = jsonObject.getString("status");
            JSONArray jsonArray = jsonObject.getJSONArray("results"); 
            for(int i=0;i<jsonArray.length();i++){
                //輸入了formatted_address
                s = jsonArray.getJSONObject(i).getString("formatted_address");
                System.out.println("formatted_address=======>"+s);
                //輸出了location裡面的lat和lng兩個值
                double s1 = jsonArray.getJSONObject(i).getJSONObject("geometry").getJSONObject("location").getDouble("lat");
                double s2 = jsonArray.getJSONObject(i).getJSONObject("geometry").getJSONObject("location").getDouble("lng");
                System.out.println("lat:"+s1 + "-----" + "lng:"+s2);
                
                JSONArray jsonArray2 = jsonArray.getJSONObject(i).getJSONArray("address_components");
                for(int j=0;j<jsonArray2.length();j++){
                    //輸出每一個long_name
                    s = jsonArray2.getJSONObject(j).getString("long_name");
                    System.out.println(s);
                    //輸出address_components裡面的types
                    JSONArray jsonArray3 = jsonArray2.getJSONObject(j).getJSONArray("types");
                    for(int k=0;k<jsonArray3.length();k++){
                        System.out.println(jsonArray3.get(k));
                    }
                }
            }
            
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

     

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.