我想从PHP获取一个数组,其中一个fileds是一个数组。 如何用jsonArray和jsonObject获取篮子的数据? (在下面的代码中,篮子是一个包含5个参数的数组)。如何在android中使用两个数组获取JSON数据
这是我的数组:
[{"orderCode":11514,"orderDate":"2017/05/21","orderPrice":"19200","fullName":"Jack","address":"addr 1","cellphone":"09151515730","basket":[{"b_qty":"4","pid":"8","b_price":"9500","b_discount":"10","title":"obj1"}]
编辑
String b_price ="";
String b_discount="";
String b_qty ="";
String title= "";
String pid="";
try {
JSONArray jsonArray = new JSONArray(data);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i);
String orderCode = object.getString("orderCode");
String orderDate = object.getString("orderDate");
String orderPrice = object.getString("orderPrice");
String fullName = object.getString("fullName");
String address = object.getString("address");
String cellphone = object.getString("cellphone");
JSONArray orderBasket = object.getJSONArray("basket");
for (int j = 0; j < orderBasket.length(); j++){
JSONObject object1 = orderBasket.getJSONObject(j);
b_qty = object1.getString("b_qty");
pid = object1.getString("pid");
b_price = object1.getString("b_price");
b_discount = object1.getString("b_discount");
title = object1.getString("title");
}
CustomOrderList customOrderList = new CustomOrderList(getApplicationContext());
customOrderList.orderCode.setText(orderCode);
customOrderList.orderDate.setText(orderDate);
customOrderList.orderTotalPrice.setText(orderPrice);
customOrderList.orderFullName.setText(fullName);
customOrderList.orderAddress.setText(address);
customOrderList.orderCellphone.setText(cellphone);
customOrderList.basketPrice.setText(b_price);
customOrderList.basketDiscount.setText(b_discount);
customOrderList.basketTitle.setText(title);
layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linearOrders.addView(customOrderList);
}
} catch (JSONException e) {
e.printStackTrace();
}
从添加您的JSON和解释越多,你想要做 –
我看你把 “菜篮子” 作为字符串什么这里。我不完全明白你想达到什么目的,这里的代码不太相关,发布你正在处理的JSON结构!然后告诉我们你想提取什么 – Mercury
我想在我的应用程序的其他细节下显示篮子的数据。]我用这段代码正确获取其他数据,但是我无法使用此代码获取篮子的数据。 – Saeidhp