我怎样才能将这个jsonarray解析为recyclerview?

时间:2018-03-30 05:21:25

标签: android json android-recyclerview

请帮帮我如何在android中的recyclerview中解析这种类型的json响应?请给我回答我搜索很多,但我没有得到任何答案

{
"data1": [
    "",
    "",
    "",
    ""
],
"data2": [
    "aaa",
    "aaa",
    "aaa",
    "aaa"
],
"data3": [
    "bbb",
    "bbb",
    "bbb",
    "bbb"
]
}

我尝试这个但只添加最后位置数据而不是所有数据

           try {
                    Log.e("res_gallery123456", responseString);

                    JSONObject jsonObj = new JSONObject(responseString);
                    BuyData buydata = new BuyData();
                    JSONArray data1=  jsonObj.getJSONArray("data1");
                    for (int i = 0;i<data1.length();i++){
                        buydata.setTxt_img(data1.getString(i));
                        JSONArray data2=jsonObj.getJSONArray("data2");

                        for (int j = 0;j<data2.length();j++){
                            buydata.setTxt_price(data2.getString(j));

                            JSONArray data3= jsonObj.getJSONArray( "data3");

                            for (int k = 0;k<data3.length();k++) {

                  buydata.setTxt_ammount(data3.getString(k));
                            }
                        }
                        buyDataList.add(buydata);
                        dialog.dismiss();
                    }
                } catch (JSONException e) {
                    dialog.dismiss();
                    e.printStackTrace();
                }

1 个答案:

答案 0 :(得分:1)

更新代码

 try {
                        Log.e("res_gallery123456", responseString);

                        JSONObject jsonObj = new JSONObject(responseString);

                        JSONArray data1=  jsonObj.getJSONArray("data1");
                        JSONArray data2=  jsonObj.getJSONArray("data2");
                        JSONArray data3=  jsonObj.getJSONArray("data3");
                        for (int i = 0;i<data1.length();i++){
                            BuyData buydata = new BuyData();
                            buydata.setTxt_img(data1.getString(i));
                            buydata.setTxt_price(data2.getString(i));
                            buydata.setTxt_ammount(data3.getString(i));

                            buyDataList.add(buydata);
                            dialog.dismiss();
                        }
                    } catch (JSONException e) {
                        dialog.dismiss();
                        e.printStackTrace();
                    }