如何在数组内的数组内读取JSON数组

时间:2019-01-07 14:44:43

标签: android json

我正在尝试阅读《最近医院的价值观》。 我正在使用google api,这是回报。 起初,我不是在阅读纬度和经度。然后,该应用程序将正常运行。现在我读了lat lang但我得到了data.size = 0;

{
    "results" : [
     {
     "geometry" : {
        "location" : {
           "lat" : 33.7724766,
           "lng" : 72.8127372
        },
        "viewport" : {
           "northeast" : {
              "lat" : 33.7739632802915,
              "lng" : 72.8141296802915
           },
           "southwest" : {
              "lat" : 33.7712653197085,
              "lng" : 72.81143171970849
           }
        }
     },
     "icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/doctor-71.png",
     "id" : "d05b6a41509c0b6dac573a6aa18453ab5ca6801e",
     "name" : "HMC Female Dispensary",
     "place_id" : "ChIJ_ddd9iyk3zgR32HNTt2Uhqw",
     "plus_code" : {
        "compound_code" : "QRC7+X3 Taxila, Pakistan",
        "global_code" : "8J5JQRC7+X3"
     },
     "reference" : "ChIJ_ddd9iyk3zgR32HNTt2Uhqw",
     "scope" : "GOOGLE",
     "types" : [ "hospital", "health", "point_of_interest", "establishment" ],
     "vicinity" : "Taxila"
  }

这是我的代码:  JSONObject obj =新的JSONObject(新的String(responseBody));

                Log.e("Error", obj.toString());
                JSONArray array = obj.getJSONArray("results");
                for (int i = 0; i < array.length(); i++) {
                    JSONObject newobj = array.getJSONObject(i);
                    final Hospital hospital = new Hospital();
                    hospital.setId(newobj.getString("id"));
                    hospital.setName(newobj.getString("name"));
                    hospital.setPlace_id(newobj.getString("place_id"));

                    //   hospital.setPhone_num();
                    if (newobj.has("rating"))
                        hospital.setRating(Double.parseDouble(newobj.getString("rating")));
                    hospital.setVicinity(newobj.getString("vicinity"));
                   JSONArray geometry = newobj.getJSONArray("geometry");

                    for (int j = 0; j < geometry.length(); j++) {
                        JSONObject jsonnewtwo = geometry.getJSONObject(j);
                        JSONArray location = jsonnewtwo.getJSONArray("location");

                        for (int k = 0; k < location.length(); k++) {


                            //get DATA here

                            JSONObject latlang = location.getJSONObject(k);

                            hospital.setLat(Double.parseDouble(latlang.getString("lat")));
                            hospital.setLng(Double.parseDouble(latlang.getString("lng")));
                            Log.e("hos", hospital.getLat() + "");
                        }
                    }



                    data.add(hospital);
                    Toast.makeText(MapsActivity.this , "Added" , Toast.LENGTH_LONG).show();

如果我不读取lat和lang,则工作正常,但如果我使用上述方法读取内部数组,则Arraylist数据的大小为0。 什么问题?

1 个答案:

答案 0 :(得分:0)

geometry是一个Json对象,您正尝试将其转换为JSONArray。检查此行。 JSONArray geometry = newobj.getJSONArray("geometry");

JSONObject geometry = newobj.getJSONObject("geometry");
JSONObject location = geometry.getJSONObject("location");

Log.w("Latitude", location.getString("lat"))
Log.w("Longitude", location.getString("lng"))
相关问题