简体   繁体   English

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

[英]How to parse nested last JSON arrays in a single object without parse second JsonArray in Android?

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

In can't get String value in third Arraylist "list":[ because is same name with Second ArrayList "list":[. 在第三个Arraylist“ list”:[中无法获取String值。因为与第二个ArrayList“ list”:[具有相同的名称。

But when I Toast Second ArrayList I see Second,third Arraylist but I can't get String third value and show nothing in Log or my listView or Toast String Value how can I access Packname? 但是,当我举起第二个ArrayList时,我看到了第二个,第三个Arraylist,但是我无法获得String的第三个值,并且在Log或listView或Toast的String值中什么也没有显示,如何访问Packname?

you have no Packname in your first array, please revise this 您的第一个数组中没有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();

please try this 请尝试这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM