使用Java解析android中的嵌套json对象

时间:2019-01-21 07:34:49

标签: java android json

我正在使用volley从我的server到Android Studio项目中获取一些数据。我需要在循环中解析ID和名称,以便填充我的Resyclerview

这是我的JSON:

  

{“ success”:true,“ 0”:{“ id”:“ 7”,“ name”:“ Asus”,“ qr”:“ qr7.png”,“ status”:null},“ 1 “:{” id“:” 17“,”名称“:” HP“,” qr“:” qr17.png“,”状态“:null},” 2“:{” id“:” 18“,” name“:” HP“,” qr“:” qr18.png“,” status“:null},” 3“:{” id“:” 19“,” name“:” HP“,” qr“:” qr19.png“,”状态“:null},” 4“:{” id“:” 20“,”名称“:” MacBook“,” qr“:” qr20.png“,”状态“:null}, “ 5”:{“ id”:“ 21”,“ name”:“ MacBook”,“ qr”:“ qr21.png”,“ status”:null},“ 6”:{“ id”:“ 22” ,“名称”:“ MacBook”,“ qr”:“ qr22.png”,“状态”:null}}

//这是到目前为止的Java:

        public void onResponse(String response) {
            this.response = response;

            try {
                JSONObject jsonResponse = new JSONObject(response);
                boolean success = jsonResponse.getBoolean("success");
                int equipId = 0;
                String itemName = null;
                if (success) {

                    int len = jsonResponse.length();

                      for (int i=0; i < len ; i++){

                          //this is where I need the 'id's' and 'names' parsed
                       mItemList.add(new EqipItem( equipId, itemName));
                     }

                    mItemAdapter = new ItemAdapter(GetData.this, mItemList);
                    mRecyclerView.setAdapter(mItemAdapter);

                } else {
                    AlertDialog.Builder builder = new AlertDialog.Builder(GetData.this);
                    builder.setMessage("Succes == false")
                            .setNegativeButton("Close Alert", null)
                            .create()
                            .show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    };

0 个答案:

没有答案