2012-04-10 97 views
0

如何解析下面的json響應。我正在開發預訂應用程序。 i。從服務器得到的響應是如何解析json響應android

{"HotelInformationResponse": { 
"@hotelId": "210985", 
    "customerSessionId": "0ABAA826-9AAF-5791-3692-A03326791310", 
"HotelSummary": { 
    "@order": "0", 
    "hotelId": 210985, 
    "name": "Seattle Marriott Waterfront", 
    "address1": "2100 Alaskan Way", 
    "city": "Seattle", 
    "stateProvinceCode": "WA", 
    "postalCode": 98121, 
    "countryCode": "US", 
    "propertyCategory": 1, 
    "hotelRating": 4, 
    "tripAdvisorRating": 4, 
    "locationDescription": "Near Seattle Aquarium", 
    "highRate": 645, 
    "lowRate": 279, 
    "latitude": 47.61016, 
    "longitude": -122.34651 
}, 
"HotelDetails": { 
    "numberOfRooms": 358, 
    "numberOfFloors": 8, 
    "checkInTime": "4:00 PM", 
    "checkOutTime": "12:00 PM", 
    "propertyInformation": "Pets not allowed Check-in time starts at 4 PM Check-out time is Noon ", 
    } 

任何方法被理解

+0

http://stackoverflow.com/questions/2818697/sending-和解析-json-in-android – 2012-04-10 11:54:06

+0

使用JSON,也許? – m0skit0 2012-04-10 12:02:12

回答

0

你可以使用JSONArray和JSONObject的手動解析它。但它很無聊,在循環運行時可能需要額外注意按鍵。最簡單和強烈推薦的方法是使用Google Gson庫自動處理。

0
private JSONObject jObject; 
jObject = new JSONObject(your response as string); 
//this will give you json object for HotelInformationResponse 
JSONObject menuObject = jObject.getJSONObject("HotelInformationResponse"); 
by this way you can get values 
String hotelId = menuObject.getString("@hotelId"); 
String customerSessionId = menuObject.getString("customerSessionId"); 
//...rest are same do same thing for HotelDetails 

額外信息Check this link

+0

儘管目標網址上的信息可能會回答問題,但我們更願意將答案放在我們網站*上,並提供鏈接作爲進一步參考。請在這裏包含您的來源的相關信息。 [閱讀更多元。](http://meta.stackexchange.com/a/8259) – animuson 2012-04-11 00:04:19

+0

@animuson - 謝謝你的建議。 – 2012-04-11 04:43:11

1

我希望這個代碼可以幫助ü

public static String Profile_response(String response){ 
      try{ 
       JSONArray jsonarray = new JSONArray("["+response+"]"); 
       JSONObject jsonobject = jsonarray.getJSONObject(0); 
       parseForcitydetail1(jsonobject.getString("HotelInformationResponse")); 


       return response; 

      }catch (Exception e) { 
       return "\"" + "Access Denied" + "\""; 
      } 
     } 

    public static void parseForcitydetail1(String res){ 
      try{ 
       JSONArray jsonarray1 = new JSONArray(res); 
       for(int i=0;i<jsonarray1.length();i++){ 
         JSONObject jsonobject = jsonarray1.getJSONObject(i); 
         Hotal_ID.add(jsonobject.getString("@hotelId")); 
         customer_ID.add(jsonobject.getString("customerSessionId")); 

      } 
parseForcitydetail2(jsonobject.getString("HotelSummary")); 


      }catch (Exception e) { 
       System.out.println(e.toString()); 
      } 
     } 

    public static void parseForcitydetail2(String res){ 
      try{ 
       JSONArray jsonarray1 = new JSONArray(res); 
       for(int i=0;i<jsonarray1.length();i++){ 
         JSONObject jsonobject = jsonarray1.getJSONObject(i); 
         Order.add(jsonobject.getString("@order")); 
         hote_ID.add(jsonobject.getString("hotelId")); 
    Name.add(jsonobject.getString("name"));      Address.add(jsonobject.getString("address1"));.... 
    City.add(jsonobject.getString("city")); 
    StateProvinceCode.add(jsonobject.getString("stateProvinceCode")); PostalCode.add(jsonobject.getString("postalCode")); 
    CountryCode.add(jsonobject.getString("countryCode")); 
    PropertyCategory.add(jsonobject.getString("propertyCategory"));      HotelRating.add(jsonobject.getString("hotelRating"));.... 
    TripAdvisorRating.add(jsonobject.getString("tripAdvisorRating")); 
    LocationDescription.add(jsonobject.getString("locationDescription")); Latitude.add(jsonobject.getString("latitude")); 
    Longitude.add(jsonobject.getString("longitude")); 
      } 

      }catch (Exception e) { 
       System.out.println(e.toString()); 
      } 
     } 

這個同樣的過程 「HotelDetails」 分析

享受!

+0

這是一些可怕的變量命名。 – cdmckay 2012-08-02 13:59:44

0
Using JSONOBject, JSONArray to contain data like JSONObject, JSONArray 
Using methods like: getJSONObject(), getString(), getXXX(). . to get data which you want. 
with XXX - data type like int, string 

您將瞭解如何解析JSON在Android中輕鬆地描述瞭如何解析JSON清楚(例)鏈接: http://www.technotalkative.com/android-json-parsing/