views:

66

answers:

1

Hi Team!

Here I am posting my Json Response below:

{"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 I want to have the following data Parsed "Phone", "Distance", "City", "Title", "State" and only "AverageRating" from the Tag "Rating".

Can anybody please help in sorting this particularly.

Thanks, David

+2  A: 

I've written about this subject before here on SO, look at it here: http://stackoverflow.com/questions/3819273/json-parsing-in-android-application/3820062#3820062

Posted some code:

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) {
        }       
}
Charlie Sheen
@Charlie: In my Response I want to get the "AverageRating" from the "Rating" TAG which is not as Array. So, still do I have to take the JSONArray for it?
david
@david: Google has a great API. You are capable to figure out how to grab the necessary info with help from the API.
Charlie Sheen