org.json.JSONObject类型的数据无法转换为JSONArray

时间:2015-04-24 19:16:00

标签: java android json

我无法理解,为什么会出现此错误:

  

04-24 22:11:51.263:W / System.err(27504):org.json.JSONException:Value   对于类型为org.json.JSONObject的数据,<!--HERE JSON VALUE-->不能   转换为JSONArray

这是我的代码:

JSONObject getProgile = null;

try {
    //get json  
    getProgile = new JSONObject(CustomHttpClient.executeHttpGet(profileGetURL).toString());
    //convert array
    JSONArray array = getProgile.getJSONArray("data");

    for (int i = 0; i < array.length(); i++) {
        JSONObject c = array.getJSONObject(i);

        //get TAG_CUSTOMER
        JSONObject customer = c.getJSONObject("Customer");

        pName = customer.getString("name");
        pLname = customer.getString("name");
    }

UPD: 我的json

{
    "status": "success",
    "data": {
        "Customer": {
            "id": "33",
            "company_id": "1",
            "name": "SDfsdf",
            "birthdate": "14.02.1989",
            "email": "dsfsdf@sf.ff",
            "photo": "/files/clients_photos/33/(null)",
            "bonuses": "50",
            "created": "2015-02-14 12:22:46",
            "modified": "2015-02-14 12:22:46",
            "ref_id": null,
            "ref_code": "6363696029",
            "banned": null,
            "ban_reason": null,
            "ban_ending": null
        },
        "CustomerVisit": [],
        "CustomerBonus": [
            {
                "id": "29",
                "customer_id": "33",
                "user_id": "4",
                "product_id": null,
                "operation": "plus",
                "amount": "50",
                "subject": "Загрузка фото при регистрации.",
                "remain": null,
                "modified": "2015-02-14 12:22:46",
                "date": "14.02.2015",
                "created": "14.02.2015 12:22"
            }
        ],
        "CustomerCar": [
            {
                "id": "41",
                "customer_id": "33",
                "car_brand_id": "9",
                "car_model_id": "11530",
                "year": "2020",
                "vin": "sdfsdfsdf",
                "photo": "",
                "number": "dsfsdf",
                "created": "2015-02-14 12:22:46",
                "modified": "2015-02-14 12:22:46",
                "car_brand_name": "BMW",
                "car_model_name": "323"
            }
        ],
        "CustomerPhone": [
            {
                "id": "41",
                "customer_id": "33",
                "phone": "+380990010222",
                "created": "2015-02-14 12:22:46",
                "modified": "2015-02-14 12:22:46"
            }
        ],
        "Insurance": [],
        "Event": [],
        "Review": [],
        "Reservation": []
    }
}

2 个答案:

答案 0 :(得分:0)

问题是您正在尝试使用JSON数组转换JSON对象,因此会发生错误。

如果您只想获得名称,那么您可以这样做:

getProgile = new JSONObject(CustomHttpClient.executeHttpGet(profileGetURL).toString());
JSONObject obj1 = getProgile.getJSONObject("data");
JSONObject obj2 = array.getJSONObject("Customer");
String name = obj2.getString("name");

答案 1 :(得分:0)

JSON显示数据是JSONObject而不是JSONArray,因此调用getProfile.getJSONObject(&#34; data&#34;)