【问题标题】:How to get JSON data with two arrays in android如何在android中使用两个数组获取JSON数据
【发布时间】:2017-08-15 17:39:51
【问题描述】:

我想从 PHP 中获取一个数组,其中一个文件是一个数组。 如何使用 jsonArray 和 jsonObject 获取篮子的数据? (在下面的代码中,篮子是一个包含 5 个参数的数组)。

这是我的数组:

 [{"orderCode":11514,"orderDate":"2017/05/21","orderPrice":"1‌​9200","fullName":"Ja‌​ck","address":"addr 1","cellphone":"09151515730","basket":[{"b_qty":"4","pid":"8‌​","b_price":"9500","‌​b_discount":"10","ti‌​tle":"obj1"}]

编辑

  String b_price ="";
    String b_discount="";
    String b_qty ="";
    String ti‌​tle= "";
    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");
                ti‌​tle = object1.getString("ti‌​tle");

            }

            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(ti‌​tle);

            layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            linearOrders.addView(customOrderList);
        }
    } catch (JSONException e) {
        e.printStackTrace();

}

【问题讨论】:

  • 添加你的 json 并解释更多你想要做什么
  • 我看到您在这里将“篮子”视为字符串。我不完全明白你想要实现什么,这里的代码不太相关,发布你正在处理的 JSON 结构!然后告诉我们你想提取什么
  • 我想在我的应用程序的其他详细信息下显示购物篮的数据。] 我使用此代码正确获取了其他数据,但我无法使用此代码获取购物篮的数据。
  • @Saeidhp 如果篮子是一个数组,那么使用这个 JSONArray orderBasket = object.getJSONArray("basket");检索
  • 这意味着我应该在 main for 中使用 for 循环?你能解释一下你的代码吗?

标签: android json


【解决方案1】:
 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");

//Now orderBasket is jsonArray
        JSONArray orderBasket = object.getJSONArray("basket");

 // So fetch all objects from orderBasket array
      for (int j = 0; j < orderBasket.length(); j++){
       JSONObject objectbsk = orderBasket.getJSONObject(j);

        String b_qty = objectbsk.getString("b_qty");
        String pid = objectbsk.getString("pid");
        String b_price = objectbsk.getString("b_price");
        String b_discount = objectbsk.getString("b_discount");
        String ti‌​tle = objectbsk.getString("ti‌​tle");
// Create a orderBasket list for Basket with these keys also as you have created for CustomOrderList and use it
}

        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.orderBasket.setText(orderBasket);

// here you can't do set text since it's jsonarray. So use basketlist to show the data`

 layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            linearOrders.addView(customOrderList);
        }

【讨论】:

  • 你可以检查你的代码吗?此代码有错误 customOrderList.orderBasket.setText(orderBasket);我在这部分将 i 更改为 j for (int j = 0; i
  • 是的,我已将 i 编辑为 j 。你告诉的第一行是我在我的回答中写到你不能 settext a jsonarray(orderbasket)。所以创建一个订单篮列表...请阅读我编辑的答案。希望它有所帮助
  • 抱歉重复这个问题。我不得不像你的代码一样编辑我的问题,但它也不起作用。请阅读我编辑的问题。
  • 输出为空。
  • @Saeidhp 请检查响应是否来自 api。
猜你喜欢
  • 1970-01-01
  • 2019-03-19
  • 2020-08-18
  • 2019-12-11
  • 1970-01-01
  • 1970-01-01
  • 2013-03-18
  • 1970-01-01
  • 2019-02-28
相关资源
最近更新 更多