繁体   English   中英

org.json.JSONException: 类型 java.lang.String 的值无法转换为 JSONObject android

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

我的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"
}

但是当我使用JSONObject response = new JSONObject(string); 它抛出org.json.JSONException ,为什么?

解析 jsonobject 抛出 jsonexception 请参考这里

使用trycatch块或throws到函数来捕获异常

try{
JSONObject response = new JSONObject(string);


}catch(JSONException e){


}

这是您示例中的工作代码。
它会打印
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();
    }
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 + "\"";
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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