首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Volley Json解析

Volley Json解析
EN

Stack Overflow用户
提问于 2018-06-06 17:49:33
回答 1查看 10关注 0票数 1

我想获取数组"list“和"items”的对象。使用下面的代码,我得到的输出不是项的值。我有一个模型班。我无法获取所有的数组对象。我已经使用了2个循环。我该怎么做呢?

代码语言:javascript
复制
{
"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
            }
        ]
    }
]

}

代码语言:javascript
复制
        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();
                    }
                }
            },
EN

回答 1

Stack Overflow用户

发布于 2018-06-06 18:07:15

我有一个解决方案

代码语言:javascript
复制
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();
                    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50717305

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档