Залп Джонс Парсинг - PullRequest
0 голосов
/ 06 июня 2018

Я хочу получить объекты массива "список", а также "элементы".С помощью приведенного ниже кода я получаю вывод как нет значений для элементов.У меня 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();
                    }
                }
            },

1 Ответ

0 голосов
/ 06 июня 2018

Я получил решение

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