[英]parsing the Json value from server side in android
我正在從服務器端獲得響應,並且很長一段時間以來,此代碼仍在工作,但現在此代碼引發了異常。
(org.json.JSONException: Value null at product of type org.json.JSONObject$1 cannot be converted to JSONArray)
{“成功”:true,“客戶”:[{“ id”:26,“客戶”:“ user1”,“產品”:[{“產品”:“檢查”},{“產品”:“排風扇“},{” product“:”固定暨終止“},{” product“:”固定暨終止“}],” job_description“:”“,” customer_rewards“:”“},{” id“:25, “ customer”:“ user2”,“ product”:[{“ product”:“凈水器”}],“ job_description”:“”,“ customer_rewards”:“”}]}
** code **
boolean status=jsonObject.getBoolean("success");
if (status) {
JSONArray jobarray=jsonObject.getJSONArray("customer");
if (jobarray.length()!=0) {
for (int i=0;i<jobarray.length();i++) {
AllJobsobject allJobsObject=new AllJobsobject();
JSONObject jobobject=jobarray.getJSONObject(i);
allJobsObject.setId(jobobject.getInt("id"));
allJobsObject.setJob_name(jobobject.getString("customer"));
JSONArray productArray = jobobject.getJSONArray("product");
ArrayList<Products> productsList = new ArrayList<Products>();
for (int n = 0; n < productArray.length(); n++) {
JSONObject productObject = productArray.getJSONObject(n);
Products product = new Products();
product.setName(productObject.getString("product"));
productsList.add(product);
}
allJobsObject.setProductsList(productsList);
datas.add(allJobsObject);
}
}
org.json.JSONException: Value null at product of type org.json.JSONObject$1 cannot be converted to JSONArray
Exception表示您的字段“ product”為null而不是json數組。 您的示例json似乎有效,並且“ product”字段是預期的數組。 有時您嘗試讀取的響應可能包含空的“ product”字段,如下所示:
{
..
"product" : null
..
}
您應該對jobobject進行null檢查。
jobobject.isNull("product")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.