简体   繁体   中英

What is the error denotes in Android(JSON) Error parsing data org.json.JSONException:

Error parsing data org.json.JSONException:

Value of type java.lang.String cannot be converted to JSONArray

JSONArray jsonArray = new JSONArray(result);
                    JSONArray ja= (JSONArray) jsonArray.get(0);
                    JSONObject jb = (JSONObject) ja.get(0);
                    //String firstvalue = jb.getString("0");
                    int secondvalue = jb.getInt("customer_id");

Myjson output should like [[{"0":"2","customer_id":"2"}]]

i am jenerating the output using the php like

$output[]= $customerid;
print(json_encode($output));

To your parsing problem its already answered @

How to call the JSON object in Android

Its a string

String secondvalue = jb.getString("CUSTOMER_ID");
try
{
   int value = Integer.parseInt("secondvalue");
   Log.i("Customer id",""+value);
}catch(NumberFormatException e)
{
  e.printStackTrace();
}

Edit:

  String myjson = "["
                  +"["
                  +"{"
                  +  " \"0\": \"2\"," 
                  +  "\"CUSTOMER_ID\": \"2\"" 
                  +   "}"
                  +  "]"
                  + "]";

Parsing

  try {
        JSONArray jsonArray = new JSONArray(myjson);
        JSONArray ja= (JSONArray) jsonArray.get(0);
        JSONObject jb = (JSONObject) ja.get(0);
        String firstvalue = jb.getString("0");
        String secondvalue = jb.getString("CUSTOMER_ID");
        int value = Integer.parseInt(secondvalue);
        Log.i("Customer id",""+value);
        Log.i("first value is",firstvalue);
        Log.i("second value is",secondvalue);
    }catch(NumberFormatException e)
     {
    e.printStackTrace();
    }catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

It simply means that the String (say, some_string) you have given as input to the new JSONArray(some_string) can not be converted to a JSON Array because the string does not follow the required json array format. An example of a proper JSON Array string is given below

[ { "title":"title_1", "name":"name_1" }, { "title":"title_2", "name":"name_2" } ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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