0

I can't understand, why I have this error:

04-24 22:11:51.263: W/System.err(27504): org.json.JSONException: Value <!--HERE JSON VALUE--> at data of type org.json.JSONObject cannot be converted to JSONArray

This is my code:

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: My 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": []
    }
}
Squonk
  • 48,735
  • 19
  • 103
  • 135
  • 1
    The return value of your web request is not a json array. print the result of the `CustomHttpClient.executeHttpGet` – Rod_Algonquin Apr 24 '15 at 19:17
  • You are trying to convert a JSONObject into a JSONArray. It's simply not possible. – Jaec Apr 24 '15 at 19:18
  • Log your getProgile object here. – Ankit Kumar Apr 24 '15 at 19:18
  • The value keyed to `"data"` is `{"Customer": ...}`. That's a JSON object. You're trying to cast it to an array. This is the equivalent of `Map,?> m = whatever(); Object o = m; List> l = (List>) o`, which will also throw a ClassCastException. – yshavit Apr 24 '15 at 19:27
  • @Jaec what do you meet? `I have JSONobject -> conver JSONarry - > JSONObject c = array.getJSONObject(i) -> JSONObject customer = c.getJSONObject("Customer")` its possible – wqedasdqwdasdasdw Apr 24 '15 at 19:29
  • `getJSONArray("data")` fails because the 'data' field isn't a json array. It's also an object(starts with {, not [) – Thomas Apr 24 '15 at 19:31
  • Read this: http://stackoverflow.com/questions/12289844/difference-between-jsonobject-and-jsonarray – Jaec Apr 24 '15 at 19:36

2 Answers2

0

The problem is that you are trying to convert and JSON Object with JSON array therefore an error will occur.

If you just want to get the name then you can do this:

getProgile = new JSONObject(CustomHttpClient.executeHttpGet(profileGetURL).toString());
JSONObject obj1 = getProgile.getJSONObject("data");
JSONObject obj2 = array.getJSONObject("Customer");
String name = obj2.getString("name");
Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63
0

JSON shows data is JSONObject rather than JSONArray, so call getProfile.getJSONObject ("data")

Haris Qurashi
  • 2,104
  • 1
  • 13
  • 28