解析android

时间:2016-02-19 05:41:09

标签: json android-json

我从服务器端获得响应,而且这段代码工作的时间很长,但现在这段代码引起了异常。

  (org.json.JSONException: Value null at product of type org.json.JSONObject$1 cannot be converted to JSONArray)

{ “成功”:真, “客户”:[{ “ID”:26, “客户”: “USER1”, “产品”:[{ “产品”: “检查”},{ “产品”: “排气扇”},{“产品”:“修复暨终止”},{“产品”:“修复暨终止”},“job_description”:“”,“customer_rewards”:“”},{“id” :25,“customer”:“user2”,“product”:[{“product”:“Water Purifier”}],“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);
                   }
                }

1 个答案:

答案 0 :(得分:1)

org.json.JSONException: Value null at product of type org.json.JSONObject$1 cannot be converted to JSONArray

异常表示您的领域&#34;产品&#34;是null而不是json数组。您的示例json似乎有效且&#34; product&#34; field是预期的数组。可能有时候你试图阅读的回复包含null&#34; product&#34;字段如下:

{
..
"product" : null
..
}

你应该对jobobject做空检查。

jobobject.isNull("product")