解析Json数据

时间:2010-10-07 07:13:22

标签: android json parsing

我在这里发布我的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"}]}}

Noe我想拥有以下数据解析“电话”,“距离”,“城市”,“标题”,“状态”以及“评分”标签中的“AverageRating”。

任何人都可以帮忙对此进行分类。

谢谢, 大卫

1 个答案:

答案 0 :(得分: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) {
        }       
}