парсинг данных Json - PullRequest
       12

парсинг данных Json

0 голосов
/ 07 октября 2010

Здесь я публикую свой ответ 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"}]}}

Нет. Я хочу получить следующие данные: "Телефон", "Расстояние", "Город", "Заголовок", "Штат" итолько "AverageRating" из тега "Rating".

Может ли кто-нибудь, пожалуйста, помочь в сортировке этого особенно.

Спасибо, Дэвид

1 Ответ

2 голосов
/ 07 октября 2010

Я уже писал об этой теме здесь, на SO, посмотрите здесь: Разбор Json в приложении для Android

Выложил код:

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) {
        }       
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...