org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject android

7.7k views Asked by At

My json file this:

{ "count" : "1",
  "list" : [ { "address" : { "address" : "新大路128号",
            "area" : "北仑区",
            "city" : "宁波",
            "email" : " ",
            "phone" : " ",
            "province" : "浙江省",
            "tel" : "13404134296",
            "username" : "大龙"
          },
        "addressId" : "359505",
        "bId" : "664512",
        "dealflag" : "0",
        "finishflag" : "0",
        "inputDate" : "2013-12-16 09:39:20",
        "paystatus" : "0",
        "siff_books" : [ { "hall" : "默认",
              "num" : "1",
              "price" : "980",
              "sheduleId" : "202331",
              "showName" : "“好预兆”蔡琴2013上海演唱会",
              "showTime" : "2013-12-28 19:30:00",
              "site" : "上海大舞台(上海体育馆)"
            } ],
        "sycid" : "0",
        "totalNum" : "1",
        "totalPrice" : "980"
      } ],
  "status" : "1"
}

but when I use JSONObject response = new JSONObject(string); It throws org.json.JSONException,why?

3

There are 3 answers

0
Nambi On

parsing the jsonobject throws jsonexception refer here

use the try and catch block or throws to the function to catch the exception

try{
JSONObject response = new JSONObject(string);


}catch(JSONException e){


}
1
dipali On
String json = {{ "count" : "1",
  "list" : [ { "address" : { "address" : "新大路128号",
            "area" : "北仑区",
            "city" : "宁波",
            "email" : " ",
            "phone" : " ",
            "province" : "浙江省",
            "tel" : "13404134296",
            "username" : "大龙"
          },
        "addressId" : "359505",
        "bId" : "664512",
        "dealflag" : "0",
        "finishflag" : "0",
        "inputDate" : "2013-12-16 09:39:20",
        "paystatus" : "0",
        "siff_books" : [ { "hall" : "默认",
              "num" : "1",
              "price" : "980",
              "sheduleId" : "202331",
              "showName" : "“好预兆”蔡琴2013上海演唱会",
              "showTime" : "2013-12-28 19:30:00",
              "site" : "上海大舞台(上海体育馆)"
            } ],
        "sycid" : "0",
        "totalNum" : "1",
        "totalPrice" : "980"
      } ],
  "status" : "1"
}};

try {

    JSONObject obj = new JSONObject(json);

    Log.d("My App", obj.toString());
    JSONArray jsonArray = jsnobject.getJSONArray("list");
    for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject explrObject = jsonArray.getJSONObject(i);
           Log.d("My App", explrObject .toString());//this will fetch list element.
            JSONArray jsonArrayinner = explrObject .getJSONObject("siff_books");//this will fetch siff_books elements...
        for (int j = 0; j < jsonArrayinner.length(); j++) {
}

} catch (Throwable t) {
    Log.e("My App", "Could not parse malformed JSON: \"" + json + "\"";
}
2
henry4343 On

This is code for work from your example.
It will print
1. o.getcount = 1
2. 新大路128号

    String json = "{ \"count\" : \"1\","+
                    "\"list\" : [ { " +
                                    "\"address\" : { " +
                                                    "\"address\" : \"新大路128号\","+
                                                    "\"area\" : \"北仑区\","+
                                                    "\"city\" : \"宁波\","+
                                                    "\"email\" : \" \","+
                                                    "\"phone\" : \" \","+
                                                    "\"province\" : \"浙江省\","+
                                                    "\"tel\" : \"13404134296\","+
                                                    "\"username\" : \"大龙\""+
                                                    "},"+
                                    "\"addressId\" : \"359505\","+
                                    "\"bId\" : \"664512\","+
                                    "\"dealflag\" : \"0\","+
                                    "\"finishflag\" : \"0\","+
                                    "\"inputDate\" : \"2013-12-16 09:39:20\","+
                                    "\"paystatus\" : \"0\","+
                                    "\"siff_books\" : [ { " +
                                                        "\"hall\" : \"默认\","+
                                                        "\"num\" : \"1\","+
                                                        "\"price\" : \"980\","+
                                                        "\"sheduleId\" : \"202331\","+
                                                        "\"showName\" : \"“好预兆”蔡琴2013上海演唱会\","+
                                                        "\"showTime\" : \"2013-12-28 19:30:00\","+
                                                        "\"site\" : \"上海大舞台(上海体育馆)\""+
                                                    "} ]," +

                                    "\"sycid\" : \"0\","+
                                    "\"totalNum\" : \"1\","+
                                    "\"totalPrice\" : \"980\""+
                                "} ],"+
                        "\"status\" : \"1\""+
                    "}\"";

    try {
        JSONObject o = new JSONObject(json);
        Log.i("chauster", "o.getcount = "+o.getString("count"));
        Log.i("chauster", o.getJSONArray("list").getJSONObject(0).getJSONObject("address").getString("address"));

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        Log.i("chauster", "e = "+e.toString());
        e.printStackTrace();
    }