从另一个JSONobject下的JSONobject获取一个字符串

时间:2018-02-22 16:42:39

标签: android json

我的json link to json

这是获取项目的代码。我在获取 photo_reference 时收到错误。我知道照片是一个JSONarray,里面有一个JSONobject。错误是它显示没有照片的值。如何获取数组以及如何获取 photo_reference 字符串?我是JSON的新手,对我来说这很难。 Log cat也是共享的。提前谢谢。

JSONObject jsonObject = new JSONObject(response);
                        JSONArray array = jsonObject.getJSONArray("results");
                        for (int i = 0; i<array.length();i++)
                        {
                            //get the photo reference from json of google place web api
                            JSONObject o = array.getJSONObject(i);
                            Places item = new Places();

                            JSONArray photo = o.getJSONArray("photos");
                            JSONObject photos = photo.getJSONObject(0);
                            String ph_ref = photos.getString("photo_reference");


                          String id = o.getString("id");
                            String name = o.getString("name");
                            if (o.has("rating")) {
                                String rat = o.getString("rating");
                                item.setRating(rat);
                            }else{
                                item.setRating("0");
                            }

org.json.JSONException:没有照片值

Logcat

    org.json.JSONException: No value for photos
    at org.json.JSONObject.get(JSONObject.java:389)
    at org.json.JSONObject.getJSONArray(JSONObject.java:584)

更新

我的解决方案

if (o.has("photos")){
                    JSONArray photo = o.getJSONArray("photos");
                    JSONObject photos = photo.getJSONObject(0);
                    String ph_ref = photos.getString("photo_reference");
                    item.setPh_ref(ph_ref);
                    }
else
    item.setPh_ref("no photos available");

2 个答案:

答案 0 :(得分:2)

有些项目在json中没有照片对象。

所以你需要检查照片对象,

JSONArray photo = o.getJSONArray("photos");
if(photo != null) {
   JSONObject photos = photo.getJSONObject(0);
}

答案 1 :(得分:1)

有些物品没有&#34;照片&#34;内。其中之一是:

  {
     "geometry" : {
        "location" : {
           "lat" : 26.1744046,
           "lng" : 91.7769246
        },
        "viewport" : {
           "northeast" : {
              "lat" : 26.1757411802915,
              "lng" : 91.7782234802915
           },
           "southwest" : {
              "lat" : 26.1730432197085,
              "lng" : 91.77552551970849
           }
        }
     },
     "icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/lodging-71.png",
     "id" : "f66820fca7c21cb6783fe27a996af3379ed7140e",
     "name" : "OYO 5332 Parnil Palace",
     "place_id" : "ChIJ0S48-Z5ZWjcRBXSBd8gS9q4",
     "rating" : 4.5,
     "reference" : "CmRSAAAAlzfMwWtNWJb8fFFyzsqNBxyZgbfbR4IPZLP2qQVph1NBgZqEwolpKNc7rRXBAQVFX4ph31iYvCrFM77v5QqDY8gEIWHBJMdW9vqWSiMlDCDF-_r6N4AUviMXwH6xTq64EhAFOGo0wihXKEy8GPtQayxiGhS6SNxnJ6DI58p9uwhSek-ov2pLJA",
     "scope" : "GOOGLE",
     "types" : [ "lodging", "point_of_interest", "establishment" ],
     "vicinity" : "Zoo Road, Zoo Tiniali, Guwahati"
  },