如何在另一个JSON对象中检索JSON对象?

时间:2014-11-24 16:01:34

标签: java json

Guyz我是android的初学者。我的问题是如何获取内部对象的json对象。例如,如我在谷歌下面的JSON数据所示,放置api,如何获取open_now内的opening_hours对象?

"opening_hours" : {
    "open_now" : true,
    "weekday_text" : []
}

以下是我的java代码:

        // Check for all possible status
                if(status.equals("OK")){
                    // Successfully got places details
                    if (nearPlaces.results != null) {
                        // loop through each place
                        for (Place p : nearPlaces.results) {




                            HashMap<String, String> map = new HashMap<String, String>();
                            double rate=p.rating;
                            boolean ostatus=p.open_now;
                             String rvalue = String.valueOf(rate);
                            //Log.d("Rating",rvalue );
                            double latitude = p.geometry.location.lat;
                            double longitude = p.geometry.location.lng;
                            Location selected_location=new Location("locationA");
                            selected_location.setLatitude(userlat);
                            selected_location.setLongitude(userlng);
                        Location near_locations=new Location("locationA");
                            near_locations.setLatitude(latitude);
                            near_locations.setLongitude(longitude);

                        double distance=selected_location.distanceTo(near_locations);
                        double dvalue=(Math.round(distance));
                         String dsvalue = String.valueOf(dvalue);

               //   Log.d("distance", dvalue);

                            // Place reference won't display in listview - it will be hidden
                            // Place reference is used to get "place full details"
                            map.put(KEY_REFERENCE, p.reference);

                            // Place name
                            map.put(KEY_NAME, p.name);

                            map.put(KEY_EXTRA,rvalue);
                             int price= p.price_level;
                             String pc = String.valueOf(price);
                             String plevel="Inexpensive";
                             if(pc.equals("0")){
                                plevel= "Inexpensive";
                              }
                              else if(pc.equals("1")){
                                plevel= "Inexpensive";

                              }
                              else if(pc.equals("2")){
                                plevel= "Moderate";
                              }

                              else if(pc.equals("3")){
                                plevel= "Expensive";
                              }
                              else if(pc.equals("4")){
                                plevel= "Very Expensive";
                              }


                            map.put(KEY_PLEVEL,plevel);
                            String add=p.vicinity;
                            map.put(KEY_ADD, add);


                            System.out.println("open status"+String.valueOf(ostatus));
                          //  Log.d("Status",ostatus);
                           String tstat;
                            if(ostatus==false){
                                tstat="Open";
                            }
                            else{

                                tstat="Open";
                            }
                            map.put(KEY_STAT, tstat);

                       //    Log.d("price level",price);

                        map.put(KEY_DISTANCE, dsvalue+" M");
                            placesListItems.add(map);



                        }

2 个答案:

答案 0 :(得分:2)

您应该使用JSONObject:

JSONObject json = new JSONObject(data);
try{
    JSONObject openingHours = json.getJSONObject("opening_hours");
    boolean openNow = openingHours.getBoolean("open_now");
}catch(JSONException e){
    e.printStackTrace();
}

您可以阅读有关JSONObject here的更多信息。

答案 1 :(得分:1)

您的Array而不是JSONObject中有JSONObject,您的模型应该是这样的:

Class Item {
   ArrayList<String> weekday_text;
   boolean open_now;
}

现在尝试解析这个模型。

...
// reader contains your json String that comes from server
Gson gson = new GsonBuilder().create();
Item item = gson.fromJson(reader, Item.class);
// now use your item object