简体   繁体   English

Google Places API - 在Android端解码Json

[英]Google Places API - Decoding Json at the Android End

I'm fairly new to dealing with json, currently working with Google Places API to get restaurant details. 我很擅长与json打交道,目前正与Google Places API合作以获取餐厅详细信息。 I've already tried to decode it by followig articles such as 我已经尝试通过以下文章对其进行解码

but it has been of no use as my json string is quite complex as far as my understanding is concerned 但是就我的理解而言,因为我的json字符串非常复杂,所以没用

I'm getting json via Volley API using this particular function 我正在通过Volley API使用这个特殊功能获得json

public String getVolleyURL() {
        String query = null;
//            query = SEARCH_QUERY + "&location=" + lat + "," + lon + "&radius=" + radius;
            query = "&location=" + lat  + "," +longg + "&radius=" + radius;

        query = SEARCH_URL + query + "&key=" + GOOGLE_PLACE_KEY;
        Log.wtf(TAG, "Volley_URL = " + query);
        return query;
    }

which returns the following Json string 返回以下Json字符串

{
   "html_attributions" : [],
   "results" : [
      {
         "formatted_address" : "Metropolitan Life North Building, 11 Madison Ave, New York, NY 10010, Verenigde Staten",
         "geometry" : {
            "location" : {
               "lat" : 40.74169,
               "lng" : -73.9872068
            },
            "viewport" : {
               "northeast" : {
                  "lat" : 40.74190884999999,
                  "lng" : -73.98716930000001
               },
               "southwest" : {
                  "lat" : 40.74161705,
                  "lng" : -73.98731529999999
               }
            }
         },
         "icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
         "id" : "f758e0d7f68c659682afdb4b1c0749e7d3623839",
         "name" : "Eleven Madison Park",
         "opening_hours" : {
            "open_now" : false,
            "weekday_text" : []
         },
         "photos" : [
            {
               "height" : 1200,
               "html_attributions" : [
                  "\u003ca href=\"https://maps.google.com/maps/contrib/102127816559576392189/photos\"\u003eTammy Liu\u003c/a\u003e"
               ],
               "photo_reference" : "CoQBdwAAACwNCAsIpt6Li3P49bVZfSX8yDOA3QTsPsgc52jYRliBNBEqboYcE409Wf-ZYIae__HbKvaW1Xed-lnKjr0dus2Gbnn1Y8PTH_7HX89aiDo4lTASyuU8N7DxHndhiAXPqY-ZKozOzLcSYA7tP50_-caFILD36WpSQJIEWSPu3cgzEhCQ7c2ltmabHZsR88s-gc8kGhShA5LDudy3ZvcDAjalj9yn_KR7bw",
               "width" : 1800
            }
         ],
         "place_id" : "ChIJEWbXz6ZZwokRLKmKrtPfVFY",
         "price_level" : 4,
         "rating" : 4.5,
         "reference" : "CnRmAAAAWyhPb38cmZ765RA05cPPZkYqZbRGtDOOQi8d5yNuwCrvezX4liYo6DFNOdr8c-v2wwwnD9OqODteeO9oto9wNGI3F0ORvjKYCAwaNwGOMc2x8JYUljND4XmG41s0d44OXWxzloUQgRubjuFEDpTkhBIQAeucZk-7vCj6xP76Mkwt2RoUMaR7pkhrawYFttRUBIYellvYoJc",
         "types" : [ "restaurant", "bar", "food", "point_of_interest", "establishment" ]
      }
   ],
   "status" : "OK"
}

Any sort of help would REALLY be appreciated! 任何形式的帮助真的很值得赞赏! Thank you 谢谢

Have you tried this ? 你试过这个吗? JSON is a very common, standard format so there are tons of resources for interfacing with them. JSON是一种非常常见的标准格式,因此有大量资源可以与它们进行交互。 The "rules" of JSONs can be seen here . 这里可以看到JSON的“规则”。

suppose if u want to decode formatted address from json response try this: 假如你想从json响应中解码formatted address ,试试这个:

Things to note: formatted address address comes under "results" key in your json response 注意事项:格式化的地址在json响应中的“results”键下

JSONObject reader = new JSONObject(query); JSONObject reader = new JSONObject(query);

JSONObject sys = reader.getJSONObject("results"); JSONObject sys = reader.getJSONObject(“results”);

address = sys.getString("formatted_address"); address = sys.getString(“formatted_address”);

so finally address will have value "Metropolitan Life North Building, 11 Madison Ave" 所以最终地址将具有价值“大都会生活北大厦,麦迪逊大街11号”

This code should help you to parse data from google places api. 此代码应该可以帮助您解析来自google places api的数据。 happy code ! 快乐的代码!

 String reference;
 String vicinity;
 String rating;
JSONObject reader = new JSONObject(response);
JSONArray data = reader.getJSONArray("results");           
for (int i = 0; i < data.length(); i++) {
JSONObject place = (JSONObject) data.get(i);
 String idplace = place.getString("id");
 String name = place.getString("name");
 Double lat = place.getJSONObject("geometry").getJSONObject("location").getDouble("lat");
  Double lng = place.getJSONObject("geometry").getJSONObject("location").getDouble("lng");
 if(place.has("photos")) {
    reference = place.getJSONArray("photos").getJSONObject(0).getString("photo_reference");
                        }
                        else
                        {

                            reference =null;
                        }
                        if(place.has("vicinity")) {
                            //it has it, do appropriate processing
                             vicinity = place.getString("vicinity");
                        }
                        else
                        {

                            vicinity =null;
                        }

                        if(place.has("rating")) {
                            //it has it, do appropriate processing
                            rating = String.valueOf(place.getDouble("rating"));
                        }
                        else
                        {

                            rating =null;
                        }
                        // String vicinity = place.getString("vicinity");
                        //String rating = String.valueOf(place.getDouble("rating"));

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

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM