2016-07-26 56 views
1

我對處理json很新,目前正在使用Google Places API獲取餐廳詳情。我已經嘗試過通過followig物品,如Google Places API - 在Android端解碼Json

解碼它,但它一直沒有用我的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; 
    } 

返回以下JSON字符串通過排球API獲取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" 
} 

任何形式的幫助真的很感謝!謝謝

+0

使用像gson這樣的API來解析json並獲取所需的值。 – lsiva

回答

0

你試過this? JSON是一種非常常見的標準格式,因此有大量的資源可以與它們連接。 JSON的「規則」可以看到here

0

想如果u想從JSON響應解碼formatted address試試這個:

注意事項:格式化地址地址來「結果」項下在你的JSON響應

的JSONObject讀卡器=新的JSONObject(查詢) ;

JSONObject sys = reader.getJSONObject(「results」);

address = sys.getString(「formatted_address」);

所以最終解決將有值「大都會人壽北樓,11麥迪遜大道」

0

此代碼應幫助您從谷歌Places API的分析數據。 快樂代碼!

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(); 
       } 

     }