2012-06-21 143 views
0

我在使用JSON時出現波紋異常。如何解決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(); 
} 
} 

爲什麼IA m如果這個錯誤?如果還有其他問題,我仍然有同樣的問題。 我搜索了很多網站,但我沒有得到答案。 請幫助...

+0

punchtime初始化在哪裏? –

+0

你有沒有在jsonlint.com上檢查過你的json –

+0

大概沒有直接關係,但是你會得到一個NullPointerException'if(punchtime == null ...'當你調用'punchtime.getString(date_new) –

回答

0

的幾件事情在這裏:

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

你覺得這是怎麼回事,如果if語句滿足這個第一條件,發生在這裏?它將是空的。在一個null對象上調用一個方法會給你一個NullPointerException。

其次,你可以發佈你試圖解析的JSON字符串嗎?有可能是JSON字符串中存在錯誤。