2010-10-07 164 views
0

我在这里发布我下面的JSON响应:解析JSON数据

{"ResultSet": 
    {"Result":[ 
     {"Phone":"(650) 473-9999", 
     "Distance":"2.59", 
     "MapUrl":"http://maps.yahoo.com/maps_result?q1=441+Emerson+St+Palo+Alto+CAgid1=28734629", 
     "Categories": 
      {"Category":[ 
       {"content":"Salad Restaurants", 
       "id":"96926225"}, 
       {"content":"Vegetarian Restaurants", 
       "id":"96926239"}, 
       {"content":"Pizza", 
       "id":"96926243"}, 
       {"content":"Wine", 
       "id":"96926864"}, 
       {"content":"Alcoholic Beverages", 
       "id":"96929810"}]}, 
     "City":"Palo Alto", 
     "Title":"Patxi's Chicago Pizza", 
     "State":"CA", 
     "Rating": 
      {"LastReviewDate":"1241373758", 
      "LastReviewIntro":"My husband brought me a slice of their pizza after I had heard glowing reviews from the Sarah and Vinnie morning show on Alice radio (they get theirs from the SF location). Anyway I had been very interested in trying what sounded like very special pizza. My husband and son went during a time when you can buy slices so waiting for the food wasnt an issue. I usually dont like pizza cold but by the time they got it home to me it was very luke warm. NOT A PROBLEM! I was amazed at how yummy it was and was having the hardest time NOT eating it while standing in the kitchen leaning over the pizza-to-go box! Im not sure of what the name of the slice was except that it did have sausage and possibly spinach in it. This was a week ago and Ive been craving it ever since. When we do go back we will either order ahead to save on the wait or go during pizza slice hours. Ive heard they also do NY style for those of you thin crust fans! Check it out!!", 
      "TotalReviews":"27", 
      "TotalRatings":"27", 
      "AverageRating":"4.5"}, 
     "Address":"441 Emerson St", 
     "Latitude":"37.445242", 
     "Longitude":"-122.163427"}]}} 

野老我想有以下数据中解析“手机”,“距离”,“城市”,“标题”,“国家“以及标签”评级“中的”平均评分“。

任何人都可以请帮忙分拣这个特别。

谢谢, 大卫

+0

JSON是一个**设置**。顺序对它并不重要。你为什么需要对它们进行分类? – qrtt1 2010-10-07 07:15:53

+0

@qrtt:他想解析他提到的具体信息。 – Wroclai 2010-10-07 07:51:29

回答

2

我已经写之前,这里SO这个问题,看看这里: Json Parsing in Android Application

发布一些代码:

String phone = null; 
String distance = null; 
String city = null; 
String title = null; 
String state = null; 
String aveRat = null; 

JSONObject jsonObject = new JSONObject(yourResponse); 
JSONArray infoArray = jsonObject.getJSONArray("Result"); 
JSONArray ratingArray = jsonObject.getJSONArray("Rating"); 

for (int i = 0; i < infoArray.length(); i++) { 
    try {  
     phone = infoArray.getJSONObject(i).optString("Phone"); 
     distance = infoArray.getJSONObject(i).optString("Distance"); 
     city = infoArray.getJSONObject(i).optString("City"); 
     title = infoArray.getJSONObject(i).optString("Title"); 
     state = infoArray.getJSONObject(i).optString("State"); 
     aveRat = ratingArray.getJSONObject(i).optString("AverageRating");   
     } catch (JSONException e) { 
     }  
} 
+0

在我的回应中,我想从“评分”标签中获得“平均评分”,而不是将其作为数组。那么,我仍然必须为此使用JSONArray? – 2010-10-07 10:59:05

+0

@david:Google有一个很棒的API。你有能力弄清楚如何在API的帮助下获取必要的信息。 – Wroclai 2010-10-07 11:55:32