Saravanan Selvam
Saravanan Selvam

Reputation: 173

parsing the Json value from server side in android

I am getting the response from server side and also long days this code is worked but now this code rise the exception.

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

{"success":true,"customer":[{"id":26,"customer":"user1","product":[{"product":"Inspection"},{"product":"Exhaust Fan"},{"product":"fixing cum Termination"},{"product":"fixing cum Termination"}],"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);
                   }
                }

Upvotes: 0

Views: 80

Answers (1)

Fuat Coşkun
Fuat Coşkun

Reputation: 1055

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

Exception says that your field "product" is null instead of a json array. Your example json seems valid and "product" field is an array as expected. Probably sometimes the response you are trying to read contains null "product" field as below :

{
..
"product" : null
..
}

You should do null check on jobobject.

jobobject.isNull("product")

Upvotes: 1

Related Questions