简体   繁体   English

如何解析json响应android

[英]How to parse json response android

How to parse this below json response.如何解析下面的 json 响应。 I am developing a booking app.我正在开发一个预订应用程序。 The response i got from the server is我从服务器得到的响应是

 {"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  ",
   }

Any method is appreciated任何方法表示赞赏

I hope this code can help u我希望这段代码可以帮助你

 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());
            }
        }

This Same process with "HotelDetails" Parsing这个与“HotelDetails”解析相同的过程

Enjoy !享受 !

You may use JSONArray and JSONObject to parse it manually.您可以使用 JSONArray 和 JSONObject 手动解析它。 but its boring and may require extra attention to keys while running in loops.但它很无聊,在循环运行时可能需要特别注意按键。 The easiest and highly recommended way is to use Google Gson library to handle this automatically.最简单且强烈推荐的方法是使用Google Gson库自动处理此问题。

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

for extra information Check this link额外信息检查此链接

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

You will understand how to parse JSON in Android easily with the link which describes how to parse JSON clearly (Example): http://www.technotalkative.com/android-json-parsing/您将通过描述如何清楚地解析JSON的链接轻松了解如何解析Android中的JSON(示例): http://www.technotalkative.com/android-json-parsing/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM