I'm using this library to make a pinned section listview. My API response is below. I need to parse the nested jsonarray but the problem is I'm getting the last object only inside the second loop for JSONArray "products" such that I'm getting same 2 list row item for the first section header instead of 2 different list row item . How to parse the nested jsonarray and add it to model class?
{
"all_cart_products": [
{
"seller_id": "3",
"seller_name": "Avik Roy",
"email": "[email protected]",
"seller_image": "http://104.131.83.218/makeoffer/upload/userimage/1491225073_ajeet_1000016806.jpg",
"products": [
{
"id": "7",
"product_user_id": "3",
"name": "rtutyikuyliou",
"desc": "Ytuykloiu sadfvdsbhdf fvdn dfjntgfkmhygdd dfsdhbgdf asfsedgdrjn sfvdsbhdf sfaswg adaswfg ADXAV adcasvfs adaswfsde safds",
"quantity": "1",
"unit_price": "100.00",
"total_price1": 100,
"total_price": "100.00",
"itemImage": "http://104.131.83.218/makeoffer/upload/product/1489739799Manab.jpg",
"totalquantity": "48"
},
{
"id": "1",
"product_user_id": "3",
"name": "product1",
"desc": "Ewfdewfs dvdfsovdfjudf bvdfofidksopdfb dfbldfjbldfb fdbdfkljbdfb dfbfkdjbdfb dfbdfjklbdfb dfbldfjkdf bdflkbjdfbdf bdfkljbdfb dfbldfkjbdf bdfbldfjkbdf bdfklbjdfdf bdfbjdflbdf bdf",
"quantity": "1",
"unit_price": "200.00",
"total_price1": 200,
"total_price": "200.00",
"itemImage": "http://104.131.83.218/makeoffer/upload/product/1489737382Manab.jpg",
"totalquantity": "50"
}
]
},
{
"seller_id": "11",
"seller_name": "Kartik roy",
"email": "[email protected]",
"seller_image": "http://104.131.83.218/makeoffer/upload/userimage/14902548541490254849295.jpg",
"products": [
{
"id": "12",
"product_user_id": "11",
"name": "Titli ghh\n",
"desc": "uh ghd Ff hg hi JJ jf TD ghh",
"quantity": "1",
"unit_price": "10.00",
"total_price1": 10,
"total_price": "10.00",
"itemImage": "http://104.131.83.218/makeoffer/upload/product/1490254269myprod.jpg",
"totalquantity": "5"
}
]
}
],
"total_qty": 1,
"Ack": 1
}
private void prepareData(){
if(arrayList!=null )arrayList.clear();
StringRequest stringRequest = new StringRequest(Request.Method.POST, "http://www.example.com/webservice/getAllCartProducts",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
String childimage="", childtitle="", childqty="", childprice="";
System.out.println("sammy_response "+response);
try {
JSONObject jObj = new JSONObject(response);
if(jObj.getInt("Ack")==1){
JSONArray jsonArray = jObj.getJSONArray("all_cart_products");
for(int i=0;i<jsonArray.length();i++){
JSONObject main = jsonArray.getJSONObject(i);
model = new Model(); model.setHeaderimage(main.getString("seller_image")); model.setHeadertext(main.getString("seller_name")); model.setHeaderid(main.getString("seller_id"));
JSONArray jarr = main.getJSONArray("products");
for(int j=0; j<jarr.length(); j++){
JSONObject obj = jarr.getJSONObject(j);
/*childimage = obj.getString("itemImage");
childtitle = obj.getString("name");
childqty = obj.getString("quantity");
childprice = obj.getString("total_price");*/ model.setChildimage(obj.getString("itemImage")); model.setChildtitle(obj.getString("name")); model.setChildqty(obj.getString("quantity")); model.setChildprice(obj.getString("total_price"));
arrayList.add(model);
}
/* model.setChildimage(childimage);
model.setChildtitle(childtitle);
model.setChildqty(childqty);
model.setChildprice(childprice);*/
}
}
mAdapter.notifyDataSetChanged();
} catch (JSONException e) {
// JSON error
System.out.println("sammy_JSONError "+e);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if (error instanceof TimeoutError) {
Toast.makeText(MainActivity.this,getString(R.string.tooslow),Toast.LENGTH_LONG).show();
}else if (error instanceof NoConnectionError){
Toast.makeText(MainActivity.this,getString(R.string.nointernet),Toast.LENGTH_LONG).show();
}else if (error instanceof AuthFailureError) {
System.out.println("sammy_AuthFailureError "+error);
} else if (error instanceof ServerError) {
System.out.println("sammy_ServerError "+error);
} else if (error instanceof NetworkError) {
System.out.println("sammy_NetworkError "+error);
} else if (error instanceof ParseError) {
System.out.println("sammy_ParseError "+error);
}
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("user_id", "7");
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
requestQueue.add(stringRequest);
stringRequest.setRetryPolicy(new DefaultRetryPolicy(
10000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
}
You can easily parse this JSON using three model objects given below
AllCartProduct class
Example class
Product class
Then using the GSON library you can parse it to the model object