无法获取json数据

时间:2016-09-16 18:01:05

标签: android json

好的,所以我正在尝试从这个网址中获取一些json数据;

https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJcUQQbsYJGToRnGdYmvWmmRs&key=API_KEY

但是对于许多标签来说,logcat说“没有价值”。 如果您在浏览器中看到JSON响应,则这些数据清晰可见。

代码段:

public void asd(String id){
    StringRequest sr = new StringRequest(Request.Method.GET, id, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try {
                JSONObject obj =new JSONObject(response);
                Log.i("response",response);
                obj.get("international_phone_number");
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.i("sdsd",error.toString());
        }
    });
    requestQueue.add(sr);
}

logcat的:

org.json.JSONException: No value for international_phone_number
09-16 23:13:59.326 7678-7678/com.example.tanmay.zomato W/System.err:     at org.json.JSONObject.get(JSONObject.java:389)
09-16 23:13:59.326 7678-7678/com.example.tanmay.zomato W/System.err:     at com.example.tanmay.zomato.MainActivity$1.onResponse(MainActivity.java:140)
09-16 23:13:59.326 7678-7678/com.example.tanmay.zomato W/System.err:     at com.example.tanmay.zomato.MainActivity$1.onResponse(MainActivity.java:134)
09-16 23:13:59.326 7678-7678/com.example.tanmay.zomato W/System.err:     at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
09-16 23:13:59.326 7678-7678/com.example.tanmay.zomato W/System.err:     at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
09-16 23:13:59.326 7678-7678/com.example.tanmay.zomato W/System.err:     at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
09-16 23:13:59.326 7678-7678/com.example.tanmay.zomato W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
09-16 23:13:59.326 7678-7678/com.example.tanmay.zomato W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
09-16 23:13:59.326 7678-7678/com.example.tanmay.zomato W/System.err:     at android.os.Looper.loop(Looper.java:148)
09-16 23:13:59.326 7678-7678/com.example.tanmay.zomato W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5417)
09-16 23:13:59.326 7678-7678/com.example.tanmay.zomato W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
09-16 23:13:59.326 7678-7678/com.example.tanmay.zomato W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
09-16 23:13:59.326 7678-7678/com.example.tanmay.zomato W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
09-16 23:13:59.455 7678-7678/com.example.tanmay.zomato I/response: {
                                                                      "html_attributions" : [],
                                                                      "result" : {
                                                                         "address_components" : [
                                                                            {
                                                                               "long_name" : "Nandankanan Road",
                                                                               "short_name" : "Nandankanan Rd",
                                                                               "types" : [ "route" ]
                                                                            },
                                                                            {
                                                                               "long_name" : "Gajapati Nagar",
                                                                               "short_name" : "Gajapati Nagar",
                                                                               "types" : [ "sublocality_level_1", "sublocality", "political" ]
                                                                            },
                                                                            {
                                                                               "long_name" : "Bhubaneswar",
                                                                               "short_name" : "Bhubaneswar",
                                                                               "types" : [ "locality", "political" ]
                                                                            },
                                                                            {
                                                                               "long_name" : "Khordha",
                                                                               "short_name" : "Khordha",
                                                                               "types" : [ "administrative_area_level_2", "political" ]
                                                                            },
                                                                            {
                                                                               "long_name" : "Odisha",
                                                                               "short_name" : "OD",
                                                                               "types" : [ "administrative_area_level_1", "political" ]
                                                                            },
                                                                            {
                                                                               "long_name" : "India",
                                                                               "short_name" : "IN",
                                                                               "types" : [ "country", "political" ]
                                                                            },
                                                                            {
                                                                               "long_name" : "751012",
                                                                               "short_name" : "751012",
                                                                               "types" : [ "postal_code" ]
                                                                            }
                                                                         ],
                                                                         "adr_address" : "\u003cspan class=\"street-address\"\u003eNandankanan Rd\u003c/span\u003e, \u003cspan class=\"extended-address\"\u003eGajapati Nagar\u003c/span\u003e, \u003cspan class=\"locality\"\u003eBhubaneswar\u003c/span\u003e, \u003cspan class=\"region\"\u003eOdisha\u003c/span\u003e \u003cspan class=\"postal-code\"\u003e751012\u003c/span\u003e, \u003cspan class=\"country-name\"\u003eIndia\u003c/span\u003e",
                                                                         "formatted_address" : "Nandankanan Rd, Gajapati Nagar, Bhubaneswar, Odisha 751012, India",
                                                                         "formatted_phone_number" : "0674 651 0407",
                                                                         "geometry" : {
                                                                            "location" : {
                                                                               "lat" : 20.303133,
                                                                               "lng" : 85.82299499999999
                                                                            },
                                                                            "viewport" : {
                                                                               "northeast" : {
                                                                                  "lat" : 20.30319164999999,
                                                                                  "lng" : 85.82321345
                                                                               },
                                                                               "southwest" : {
                                                                                  "lat" : 20.30295705,
                                                                                  "lng" : 85.82233964999999
                                                                               }
                                                                            }
                                                                         },
                                                                         "icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/cafe-71.png",
                                                                         "id" : "b2e435456f40489ebf4de9c82ff8dc3eb0179a2c",
                                                                         "international_phone_number" : "+91 674 651 0407",
                                                                         "name" : "Cafe Coffee Day - Nandan Kanan Road",
                                                                         "opening_hours" : {
                                                                            "open_now" : false,
                                                                            "periods" : [
                                                                               {
                                                                                  "close" : {
                                                                                     "day" : 0,
                                                                                     "time" : "2300"
                                                                                  },
                                                                                  "open" : {
                                                                                     "day" : 0,
                                                                                     "time" : "0900"
                                                                                  }
                                                                               },
                                                                               {
                                                                                  "close" : {
                                                                                     "day" : 1,
                                                                                     "time" : "2300"
                                                                                  },
                                                                                  "open" : {
                                                                                     "day" : 1,
                                                                                     "time" : "0900"
                                                                                  }
                                                                               },
                                                                               {
                                                                                  "close" : {
                                                                                     "day" : 2,
                                                                                     "time" : "2300"
                                                                                  },
                                                                                  "open" : {
                                                                                     "day" : 2,
                                                                                     "time" : "0900"
                                                                                  }
                                                                               },
                                                                               {
                                                                                  "close" : {
                                                                                     "day" : 3,
                                                                                     "time" : "2300"
                                                                                  },
                                                                                  "open" : {
                                                                                     "day" : 3,
                                                                                     "time" : "0900"
                                                                                  }
                                                                               },
                                                                               {
                                                                                  "close" : {
                                                                                     "day" : 4,
                                                                                     "time" : "2300"
                                                                                  },
                                                                                  "open" : {
                                                                                     "day" : 4,
                                                                                     "time" : "0900"
                                                                                  }
                                                                               },
                                                                               {
                                                                                  "close" : {
                                                                                     "day" : 5,
                                                                                     "time" : "2300"
                                                                                  },
                                                                                  "open" : {
                                                                                     "day" : 5,
                                                                                     "time" : "0900"
                                                                                  }
                                                                               },
                                                                               {
                                                                                  "close" : {
                                                                                     "day" : 6,
                                                                                     "time" : "2300"
                                                                                  },
09-16 23:13:59.455 7678-7678/com.example.tanmay.zomato W/System.err: org.json.JSONException: No value for international_phone_number
09-16 23:13:59.455 7678-7678/com.example.tanmay.zomato W/System.err:     at org.json.JSONObject.get(JSONObject.java:389)
09-16 23:13:59.455 7678-7678/com.example.tanmay.zomato W/System.err:     at com.example.tanmay.zomato.MainActivity$1.onResponse(MainActivity.java:140)
09-16 23:13:59.455 7678-7678/com.example.tanmay.zomato W/System.err:     at com.example.tanmay.zomato.MainActivity$1.onResponse(MainActivity.java:134)
09-16 23:13:59.456 7678-7678/com.example.tanmay.zomato W/System.err:     at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
09-16 23:13:59.456 7678-7678/com.example.tanmay.zomato W/System.err:     at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
09-16 23:13:59.456 7678-7678/com.example.tanmay.zomato W/System.err:     at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
09-16 23:13:59.456 7678-7678/com.example.tanmay.zomato W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
09-16 23:13:59.456 7678-7678/com.example.tanmay.zomato W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
09-16 23:13:59.456 7678-7678/com.example.tanmay.zomato W/System.err:     at android.os.Looper.loop(Looper.java:148)
09-16 23:13:59.456 7678-7678/com.example.tanmay.zomato W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5417)
09-16 23:13:59.456 7678-7678/com.example.tanmay.zomato W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
09-16 23:13:59.456 7678-7678/com.example.tanmay.zomato W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
09-16 23:13:59.456 7678-7678/com.example.tanmay.zomato W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
09-16 23:19:34.592 7678-7684/com.example.tanmay.zomato W/art: Suspending all threads took: 7.170ms
09-16 23:21:16.246 7678-7684/com.example.tanmay.zomato W/art: Suspending all threads took: 6.563ms
09-16 23:25:28.666 7678-7684/com.example.tanmay.zomato W/art: Suspending all threads took: 6.054ms

这些函数只是我正在使用的代码的简要版本。

2 个答案:

答案 0 :(得分:0)

您的错误表明密钥不在响应的顶层,而不是在数据的任何位置都不存在。

您必须进入results键。

JSONObject obj =new JSONObject(response);
JSONObject result = response.getJSONObject("results");
String inumber = result.getString("international_phone_number");

答案 1 :(得分:0)

尝试getJSONObject,只需获取。

如下:

@Override
public void onResponse(String response) {
    try {
        JSONObject obj =new JSONObject(response);
        JSONObject results = object.getJSONObject("results");
        String internationalNumber = result.getString("international_phone_number");
    } catch (JSONException e) {
        e.printStackTrace();
}

}