0

使用 JSON 时出现以下异常。

06-21 18:59:43.418: W/System.err(682): org.json.JSONException: Value null at PunchTime of type org.json.JSONObject$1 cannot be converted to JSONObject
06-21 18:59:43.428: W/System.err(682):  at org.json.JSON.typeMismatch(JSON.java:96)
06-21 18:59:43.428: W/System.err(682):  at org.json.JSONObject.getJSONObject(JSONObject.java:573)
06-21 18:59:43.428: W/System.err(682):  at com.android.epay.TestClass1.getEmployeeList(TestClass1.java:281)
06-21 18:59:43.428: W/System.err(682):  at com.android.epay.TestClass1$GroupPunch_task.doInBackground(TestClass1.java:214)
06-21 18:59:43.438: W/System.err(682):  at com.android.epay.TestClass1$GroupPunch_task.doInBackground(TestClass1.java:1)
06-21 18:59:43.438: W/System.err(682):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
06-21 18:59:43.438: W/System.err(682):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
06-21 18:59:43.438: W/System.err(682):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
06-21 18:59:43.438: W/System.err(682):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
06-21 18:59:43.448: W/System.err(682):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
06-21 18:59:43.448: W/System.err(682):  at java.lang.Thread.run(Thread.java:1096)

我的代码是..

j_obj = new JSONObject(responseBody);
JSONArray sites = j_obj.getJSONArray("details");
String date_new = null;
for (int i = 0; i < sites.length(); i++) {
    JSONObject list_obj = sites.getJSONObject(i);
    JSONObject type = list_obj.getJSONObject("****");
    JSONObject time = type.getJSONObject("@@@@@");
    String name = list_obj.getString("#####");
    String id = list_obj.getString("id");
    String _no = list_obj.getString("num");
    String type = type.getString("type");


    if (punchtime == null || punchtime.equals("")) {
        date_new = "Null Values";

    } else {

        year = time.getString("Year");
        month = time.getString("Month");
        day = time.getString("Day");
        hours = time.getString("Hours");
        minutes = time.getString("Minutes");
        seconds = time.getString("Seconds");
        millisec = time.getString("Milliseconds");
        date_new = month + "-" + day + "-" + year + " " + hours + ":" + minutes + ":" + seconds + ":" + millisec;
        System.out.println("New Date " + date_new);
    }

    _arrayList.add(id);
    arryList.add(name);
    _arrayList.add(_no);
    punchType_arrayList.add(type);
    punchTime_arrayList.add(date_new);

}


System.out.println("----------------------------------------");

} finally {
    httpclient.getConnectionManager().shutdown();
}
} 

为什么我收到此错误?我用了if else,还是有同样的问题。我搜索了很多网站,但我没有得到答案。请帮忙 ...

4

1 回答 1

0

这里有几件事:

 if (punchtime == null || punchtime.equals("")) {
    date_new = "Null Values";
    punchtime.getString(date_new);
 }

如果这个 if 语句的第一个条件得到满足,你认为这里会发生什么?它将为空。对 null 对象调用方法会给您一个 NullPointerException。

其次,您可以发布您要解析的 JSON 字符串吗?JSON 字符串中可能存在错误。

于 2012-06-21T14:15:18.073 回答