我使用这个library做一个固定的部分列表视图。我的API回应如下。我需要解析嵌套jsonarray但问题是我得到的最后一个对象只针对JSONArray“产品”,使得我得到相同的2列表行项目的第一节头部,而不是2个不同的列表行第二循环中项目。如何解析嵌套的jsonarray并将其添加到模型类?嵌套JsonArray对已固定的ListView解析
{
"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));
}
问题不明确? – Krish
您是否要求提供该JSON的解析器? – AlexTa
我得到每个标题的重复子元素,即'products' jsonarray内的最后一个jsonobject正在被覆盖在j-for循环中。请检查图像。 – CodeAssasins