如何在单个对象中解析嵌套的最后一个JSON数组而不解析Android中的第二个JsonArray?

时间:2017-01-30 20:17:41

标签: java android json

{
  "LIST": [
    {
      "GroupName": "گالری",
      "GroupID": "1",
      "GType": "gallery1",
      "list": [
        {
          "Title": "food",
          "RefType": "app",
          "PicUrl": "http://tv.dmedia.ir/images/icon/slide2.jpg",
          "RefID": "194"
        },
        {
          "Title": "drink ",
          "RefType": "app",
          "PicUrl": "http://tv.dmedia.ir/images/icon/slide1.jpg",
          "RefID": "199"
        }
      ]
    },
    {
      "GroupName": "ویدئوهای برگزیده",
      "GroupID": "2",
      "GType": "apk",
      "list": [
        {
          "AppID": "333",
          "Packname": "shadkami",
          "AppName": "شادکامی و مثبت اندیشی",
          "AppSize": 13066,
          "VersionCode": "0",
          "IconURL": "http://tv.dmedia.ir/paneluser/uploads/files/113.f402cee9889beb52e844012aec28ab0d.mp4/icon/becc1783d1263730b177317f4950f187.jpg",
          "IconURL2": "",
          "Specials": "0",
          "DownloadsCount": "0",
          "LikesCount": "0",
          "GroupID": "16",
          "AppPrice": 0,
          "AppAutor": null,
          "AppRate": 0,
          "GroupType": "1"
        },
        {
          "AppID": "332",
          "Packname": "afzayeshetemad",
          "AppName": "افزایش اعتماد به نفس",
          "AppSize": 25132,
          "VersionCode": "0",
          "IconURL": "http://tv.dmedia.ir/paneluser/uploads/files/113.b52e30b721705d884832ffc7aa533375.mp4/icon/63c178ab34267b3e25691a7c1b6914c8.jpg",
          "IconURL2": "",
          "Specials": "0",
          "DownloadsCount": "0",
          "LikesCount": "0",
          "GroupID": "16",
          "AppPrice": 0,
          "AppAutor": null,
          "AppRate": 0,
          "GroupType": "1"
        }
      ]
    }
  ]
}
    try {
        JSONObject object = new JSONObject(respond);

        JSONArray jsonArrayLIST = object.getJSONArray("LIST");

        ArrayList<ListViewMain> listmain = new ArrayList<>();

        ListViewMainAdapter adapter = new ListViewMainAdapter(MainActivity.this, R.layout.list_view_main, listmain);
        list_view_main.setAdapter(adapter);

        for (int i = 0; i < jsonArrayLIST.length(); i++) {

            JSONObject objectLIST = jsonArrayLIST.getJSONObject(i);

            JSONArray jsonArraylist = objectLIST.getJSONArray("list");

            Toast.makeText(MainActivity.this, jsonArraylist.toString(), Toast.LENGTH_SHORT).show();

            for (int j = 0; j < jsonArraylist.length(); j++) {

                JSONObject objectlist = jsonArraylist.getJSONObject(j);

                String Packname= objectlist.getString("Packname");

                Log.i("Log", "SHOW Packname " + Packname);

                listmain.add(new ListViewMain(Packname));

                adapter.notifyDataSetChanged();
            }
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

在第三个Arraylist“list”中无法获取String值:[因为与Second ArrayList“list”同名:[。

但是当我Toast Second ArrayList时,我看到第二个,第三个Arraylist,但是我无法获得String第三个值,并且在Log或我的listView或Toast字符串值中没有显示任何内容 我怎样才能访问Packname?

1 个答案:

答案 0 :(得分:0)

你的第一个数组中没有Packname,请修改此

for (int j = 0; j < jsonArraylist.length(); j++) {
  JSONObject objectlist = jsonArraylist.getJSONObject(j);
  if(objectlist.has("Packname")){
    String Packname= objectlist.getString("Packname");
    Log.i("Log", "SHOW Packname " + Packname);
    listmain.add(new ListViewMain(Packname));
  }
}
adapter.notifyDataSetChanged();

请试试这个