Android JsonObject 检索

Android JsonObject Retrieve

需要 Json 的帮助,我混淆了 JsonArray 和 JsonObject。我想得到 formatted_phone_number

{
"html_attributions" : [],
"result" : {
  "address_components" : [
     {
        "long_name" : "Jalan Hang Tuah",
        "short_name" : "Jalan Hang Tuah",
        "types" : [ "route" ]
     },
     {
        "long_name" : "Malacca",
        "short_name" : "Malacca",
        "types" : [ "locality", "political" ]
     },
     {
        "long_name" : "Malacca",
        "short_name" : "Malacca",
        "types" : [ "administrative_area_level_1", "political" ]
     },
     {
        "long_name" : "Malaysia",
        "short_name" : "MY",
        "types" : [ "country", "political" ]
     },
     {
        "long_name" : "75300",
        "short_name" : "75300",
        "types" : [ "postal_code" ]
     }
  ],
  "adr_address" : "GF Plaza Melaka 75300, \u003cspan class=\"street-address\"\u003eJalan Hang Tuah\u003c/span\u003e, \u003cspan class=\"postal-code\"\u003e75300\u003c/span\u003e \u003cspan class=\"locality\"\u003eMalacca\u003c/span\u003e, \u003cspan class=\"country-name\"\u003eMalaysia\u003c/span\u003e",
  "formatted_address" : "GF Plaza Melaka 75300, Jalan Hang Tuah, 75300 Malacca, Malaysia",
  "formatted_phone_number" : "06-283 3380",
  "geometry" : {
     "location" : {
        "lat" : 2.20419,
        "lng" : 102.246
     },
     "viewport" : {
        "northeast" : {
           "lat" : 2.205572530291502,
           "lng" : 102.2474190302915
        },
        "southwest" : {
           "lat" : 2.202874569708499,
           "lng" : 102.2447210697085
        }
     }
  },
  "icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/shopping-71.png",
  "id" : "5d05ce9257a424bf07f273b0ef364d1bc5af4ac7",
  "international_phone_number" : "+60 6-283 3380",
  "name" : "Farmasi Kota Sdn Bhd",
  "place_id" : "ChIJ0SROVNDx0TERO7ni_qrZCdY",
  "rating" : 5,
  "reference" : "CmRSAAAADNPBz3cWAdcK41-O2nZhqFcOemuzReuXzO6983nZsz8An5vvNT6OVnow-ubgARNQpnGf7sr3sjo6mwDwW6UNFGYdQn_7KoS215c96rHpX1cqMdGxchZDL_iHCO5-A1aVEhCHcLga4gbpUh2eBHTX7vwiGhT5u3MPZH7h_GLA7hRJvrS2QFEuAg",
  "reviews" : [
     {
        "aspects" : [
           {
              "rating" : 3,
              "type" : "overall"
           }
        ],
        "author_name" : "Cheng Qi",
        "author_url" : "https://www.google.com/maps/contrib/102025986299133647688/reviews",
        "language" : "zh",
        "profile_photo_url" : "https://lh6.googleusercontent.com/-UcpciRqEdas/AAAAAAAAAAI/AAAAAAAAAAA/AHalGhrBhb2yrqf8dUuj-I6-Ioz9HQtf0w/s128-c0x00000000-cc-rp-mo/photo.jpg",
        "rating" : 5,
        "relative_time_description" : "5 months ago",
        "text" : "",
        "time" : 1478565315
     }
  ],
  "scope" : "GOOGLE",
  "types" : [
     "pharmacy",
     "hospital",
     "health",
     "store",
     "point_of_interest",
     "establishment"
  ],
  "url" : "https://maps.google.com/?cid=15423097727350913339",
  "utc_offset" : 480,
  "vicinity" : "GF Plaza Melaka 75300, Jalan Hang Tuah, Malacca"
},
"status" : "OK"
}

这是我的代码。

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, URL,
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {
                            try {
                                JSONArray jsonArray = response.getJSONArray("result");

                                for (int i = 0; i < jsonArray.length(); i++) {
                                    JSONObject result = jsonArray.getJSONObject(i);


                                    String name = result.getString("name");
                                    String formatted_phone_number= result.getString("formatted_phone_number");
                                    textView.append(name + " " + " " + formatted_phone_number+ "\n");

                                }

                            } catch (JSONException e) {
                                e.printStackTrace();

                            }
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Log.e("VOLLEY ", "ERROR");
                        }
                    });
            requestQueue.add(jsonObjectRequest);

试试这个:

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, URL,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                             JSONObject result = response.getJSONObject("result");

                             String name = result.getString("name");
                             String formatted_phone_number= result.getString("formatted_phone_number");
                             textView.append(name + " " + " " + formatted_phone_number+ "\n");

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.e("VOLLEY ", "ERROR");
                    }
                });
        requestQueue.add(jsonObjectRequest);