【问题标题】:Volley Json ParsingVolley Json 解析
【发布时间】:2018-06-06 09:49:32
【问题描述】:

我想获取数组“列表”的对象以及“项目”。使用下面的代码,我得到的输出是没有项目的值。我有1个模型课。我无法获取所有数组对象。我使用了 2 个循环。我该怎么做?

{
"status": 200,
"list": [
    {
        "quot_uid": "QUOTE2018@1",
        "id": "1",
        "expiry_date": "2018-05-29",
        "created_at": "2018-05-22 11:45:58",
        "left_days": "8",
        "items": [
            {
                "ITEM_NAME": "Copper Wires",
                "UNIT": "MT",
                "qty": "5",
                "make": null
            },
            {
                "ITEM_NAME": "OFC Cables",
                "UNIT": "MT",
                "qty": "2",
                "make": null
            }
        ]
    }
]

}

        StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    progressDialog.dismiss();
                    try {
                        JSONObject jsonObject = new JSONObject(response);
                        JSONArray jsonArray = jsonObject.getJSONArray("list");
                        JSONArray jsonArray1 = jsonObject.getJSONArray("items");

                        for (int i =0 ; i < jsonArray.length();i++) {
                            for(int j=0; j< jsonArray1.length(); j++){
                                JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                                JSONObject object = jsonArray1.getJSONObject(j);
                                ListQuotation item = new ListQuotation(
                                        jsonObject1.getString("quot_uid"),
                                        jsonObject1.getString("created_at"),
                                        jsonObject1.getString("expiry_date"),
                                        jsonObject1.getString("left_days"),
                                        object.getString("ITEM_NAME"),
                                        object.getString("qty")
                                );
                               listItems.add(item);
                            }
                         }
                       recyclerView.setAdapter(adapter);
                    } catch (JSONException e) {
                        Toast.makeText(getContext(),"No Records Found",Toast.LENGTH_LONG);
                        Log.e("Error", "Failed" +e.toString());
                        e.printStackTrace();
                    }
                }
            },

【问题讨论】:

    标签: arrays json parsing android-volley


    【解决方案1】:

    我有办法

    try {
                            JSONObject jsonObject = new JSONObject(response);
                            JSONArray jsonArray = jsonObject.getJSONArray("list");
    
                            for (int i =0 ; i < jsonArray.length();i++) {
                                    JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                                    JSONArray jsonArray1 = jsonObject1.getJSONArray("items");
                                for (int j =0 ; j < jsonArray1.length();j++) {
                                    JSONObject jsonObject2 = jsonArray1.getJSONObject(j);
                                    ListQuotation item = new ListQuotation(
                                            jsonObject1.getString("quot_uid"),
                                            jsonObject1.getString("created_at"),
                                            jsonObject1.getString("expiry_date"),
                                            jsonObject1.getString("left_days"),
                                            jsonObject2.getString("ITEM_NAME"),
                                            jsonObject2.getString("qty")
    
                                    );
    
                                   listItems.add(item);
                                }
                                }
    
                           recyclerView.setAdapter(adapter);
                        } catch (JSONException e) {
                            Toast.makeText(getContext(),"No Records Found",Toast.LENGTH_LONG);
                            Log.e("Error", "Failed" +e.toString());
                            e.printStackTrace();
                        }
    

    【讨论】:

      猜你喜欢
      • 2016-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多