解析json时出错

时间:2015-07-13 10:53:20

标签: android json

这里是json的结构,它在jsonlint上有效。我只需要获取第一个块所有细节,如hostid和all。我试图解析数据,但收到一个错误。

{
    "\u0000yii\\db\\BaseActiveRecord\u0000_attributes": {
        "hostid": 234,
        "user_id": 77,
        "hometype": "house",
        "roomtype": "Entire Home",
        "accommodates": "2",
        "city": "Chandigarh",
        "street_address": "",
        "state": "Chandigarh 160017",
        "zipcode": "",
        "country": "India",
        "latitude": "30.740212629732113",
        "longitude": "76.78718557730713",
        "address": "Bank Square, 17C, Sector 17, Chandigarh, Chandigarh 160017, India",
        "bedrooms": "2",
        "beds": "2",
        "bathrooms": "1",
        "from_date": "2015-07-16",
        "to_date": "2015-08-19",
        "available": "",
        "amount_pernight": 100,
        "currency": "INR",
        "title": "AmazingSeaFacingPrivateRoom",
        "summery": "Title should highlight the key features and will attract guests to choose your listing! Add an innovative title that best describes your home.\r\n\r\nExamples:\r\n\r\n Amazing Sea Facing Private Room!\r\n Fully Furnished Villa!\r\n Free Welcome Drink on Arrival!\r\n",
        "mostcomman_facilities": "New Soap & Shampoo Sachet-:-Dry-cleaned Towel-:-Blanket-:-TV-:-Cable TV-:--:--:--:--:--:-",
        "extra_facilities": "Extra Mattress-:-Extra Blanket-:-Washer-:-Alarm clock-:-Laundry Service-:-Gym-:--:--:--:--:-",
        "features_facilities": "Smoking Allowed-:-Family & Children Friendly-:-Pets allowed-:-Wheelchair Available",
        "safety_facilities": "Fire Extinguisher-:-Smoke Detector-:-Carbon Monoxide Detector-:-",
        "num_bed": "0",
        "bed_type": 2,
        "currency_symbol": "",
        "servicefee": "",
        "cleaningfee": "",
        "person": "all",
        "landmark_location": "",
        "landmark_distance": "",
        "hostuniqueweb": "",
        "hosttype": "web",
        "extraperson": "Free",
        "breakfast": "Free",
        "Lunch": "Free",
        "Dinner": "Free",
        "Tea": "Free",
        "Coffee": "Free",
        "Airport": "Free"
    },
    "\u0000yii\\db\\BaseActiveRecord\u0000_oldAttributes": {
        "hostid": 234,
        "user_id": 77,
        "hometype": "house",
        "roomtype": "Entire Home",
        "accommodates": "2",
        "city": "Chandigarh",
        "street_address": "",
        "state": "Chandigarh 160017",
        "zipcode": "",
        "country": "India",
        "latitude": "30.740212629732113",
        "longitude": "76.78718557730713",
        "address": "Bank Square, 17C, Sector 17, Chandigarh, Chandigarh 160017, India",
        "bedrooms": "2",
        "beds": "2",
        "bathrooms": "1",
        "from_date": "2015-07-16",
        "to_date": "2015-08-19",
        "available": "",
        "amount_pernight": 100,
        "currency": "INR",
        "title": "AmazingSeaFacingPrivateRoom",
        "summery": "Title should highlight the key features and will attract guests to choose your listing! Add an innovative title that best describes your home.\r\n\r\nExamples:\r\n\r\n Amazing Sea Facing Private Room!\r\n Fully Furnished Villa!\r\n Free Welcome Drink on Arrival!\r\n",
        "mostcomman_facilities": "New Soap & Shampoo Sachet-:-Dry-cleaned Towel-:-Blanket-:-TV-:-Cable TV-:--:--:--:--:--:-",
        "extra_facilities": "Extra Mattress-:-Extra Blanket-:-Washer-:-Alarm clock-:-Laundry Service-:-Gym-:--:--:--:--:-",
        "features_facilities": "Smoking Allowed-:-Family & Children Friendly-:-Pets allowed-:-Wheelchair Available",
        "safety_facilities": "Fire Extinguisher-:-Smoke Detector-:-Carbon Monoxide Detector-:-",
        "num_bed": "0",
        "bed_type": 2,
        "currency_symbol": "",
        "servicefee": "",
        "cleaningfee": "",
        "person": "all",
        "landmark_location": "",
        "landmark_distance": "",
        "hostuniqueweb": "",
        "hosttype": "web",
        "extraperson": "Free",
        "breakfast": "Free",
        "Lunch": "Free",
        "Dinner": "Free",
        "Tea": "Free",
        "Coffee": "Free",
        "Airport": "Free"
    },
    "\u0000yii\\db\\BaseActiveRecord\u0000_related": [],
    "\u0000yii\\base\\Model\u0000_errors": null,
    "\u0000yii\\base\\Model\u0000_validators": null,
    "\u0000yii\\base\\Model\u0000_scenario": "default",
    "\u0000yii\\base\\Component\u0000_events": [],
    "\u0000yii\\base\\Component\u0000_behaviors": []
}

这是我解析此结构的代码:

JSONObject obj = new JSONObject(jsonStr);
                System.out.println("hello");
                JSONObject ob=obj.getJSONObject("\u0000yii\\db\\BaseActiveRecord\u0000_attributes");
                System.out.println("hello123");

                        home_type=ob.getString("home_type");
                        Log.v("home type is", home_type);

我在logcat中收到以下错误。这是logcat的详细信息。请帮助:

07-13 16:21:07.868: W/System.err(4678): org.json.JSONException: Value [] of type org.json.JSONArray cannot be converted to JSONObject
07-13 16:21:07.868: W/System.err(4678):     at org.json.JSON.typeMismatch(JSON.java:111)
07-13 16:21:07.869: W/System.err(4678):     at org.json.JSONObject.<init>(JSONObject.java:159)
07-13 16:21:07.869: W/System.err(4678):     at org.json.JSONObject.<init>(JSONObject.java:172)
07-13 16:21:07.869: W/System.err(4678):     at com.app.hostguestapp.Preview_form$Preview_form_data.doInBackground(Preview_form.java:91)
07-13 16:21:07.869: W/System.err(4678):     at com.app.hostguestapp.Preview_form$Preview_form_data.doInBackground(Preview_form.java:1)
07-13 16:21:07.869: W/System.err(4678):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
07-13 16:21:07.869: W/System.err(4678):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
07-13 16:21:07.870: W/System.err(4678):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
07-13 16:21:07.870: W/System.err(4678):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
07-13 16:21:07.870: W/System.err(4678):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)

3 个答案:

答案 0 :(得分:1)

您必须将此JSONObject ob=obj.getJSONObject替换为

obj.getJSONArray .. because("\u000....")是图像描述的数组

更多有用的教程检查this

enter image description here图片source

并且作为rui santos,您应该将其修复为此ob.getString("hometype");

答案 1 :(得分:0)

问题在于你的json响应。前两个元素是jsonObjects,但第三个元素“\ u0000yii \ db \ BaseActiveRecord \ u0000_related”是jsonArray。类似地,“\ u0000yii \ base \ Component \ u0000_events”和“\ u0000yii \ base \ Component \ u0000_behaviors”也是jsonArray。

答案 2 :(得分:0)

try{

JSONObject obj = new JSONObject(jsonStr);
JSONObjet subJsnObj = obj.getJSONObject("\u0000yii\\db\\BaseActiveRecord\u0000_attributes");
String hostId = subJsnObj.getString("hostid");
System.out.println("JSONObject is "+ subJsnObj);

}catch(Exception e){
//exception
}