JSONException:

时间:2017-02-02 02:35:14

标签: android json

我有一些问题,在logcat中说

D/JSON Parser: ����������µM@@ÿJÌYÁ5õfyo]"dÄM]wÅ]ÿ{kXôuí4ðÌ<fR)Îd
               ña,-8vbðp@botSâ¯&©V ±4Ý~HÌBSBÓN<¸¸?sN{UÑãHIrR`ÊN´AÓ뢣Ҡ°|£äu
               `t¾r¿j3Ê+~¤çÿ-]ÏoÖõ\S1q ¥úõÔ7©ÀAT����
E/JSON Parser:Errorparsing data org.json.JSONException:
              Value����������µM@@ÿJÌYÁ5õfyo of type java.lang.String
              cannot be converted to JSONObject

这是我的代码:

 public static final String TAG_BOOK = "booking"; 
        Log.d("book: ", jObject.toString());
        try{
            int sukses = jObject.getInt(TAG_SUCCESS);
            if (sukses == 1){
                booking = jObject.getJSONArray(TAG_BOOK);
                booklist.removeAll(booklist);
                for (int i = 0;i < booking.length();i++) {
                    JSONObject obj = booking.getJSONObject(i);
                    HashMap<String,String> map = new HashMap<String, String>();
                    String id = obj.getString(TAG_ID);
                    String nKend = obj.getString(TAG_KENDARAAN);
                    String sttus = obj.getString(TAG_STATUSBOOK);

                    map.put(TAG_ID, id);
                    map.put(TAG_KENDARAAN, nKend);
                    map.put(TAG_STATUSBOOK, sttus);
                    map.put(TAG_INDEX, ""+i);

                    booklist.add(map);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

这是我的JSON对象

{
"booking": [
    {
      "ID_kendaraan": "26",
      "nama_kendaraan": "Vario 125",
      "no_plat": "DK6281AI",
      "tgl_sewa": "2016-11-02",
      "tgl_kembali": "2016-11-09",
      "nama_belakang": "Maong",
      "status_rental": "Confirmed"
    },
    {
      "ID_kendaraan": "3",
      "nama_kendaraan": "Avanza",
      "no_plat": "DK890OO",
      "tgl_sewa": "2016-11-02",
      "tgl_kembali": "2016-11-09",
      "nama_belakang": "Maong",
      "status_rental": "Booking"
    }
],
    "sukses": 1, 
    "pesan": "Booking"
}

当我使用postman尝试获取JSONobject之前的工作就像我的JSON对象之前但在Logcat中无法获取我的json对象。我是新来的,所以请帮忙,抱歉英语不好。感谢

1 个答案:

答案 0 :(得分:0)

你应该先检查

JSONObject obj = booking.getJSONObject(i);
if(obj.has("object_withno_value")) { 
    //Code if it has
}