简体   繁体   English

如何解析Android中具有多个jsonArrays的json数据?

[英]How to parse json data which has multiple jsonArrays in android?

I have JSON data like this,it is having multiple jsonArrays in it.how to parse this type of data? 我有这样的JSON数据,其中有多个jsonArrays。如何解析这种类型的数据?

{
        "result":
        [{
        "site_name":"comenius",
        "ws_url":"https://comenius-api.sabacloud.com/v1/",
        "base_url":"https://comenius.sabacloud.com/",
        "logo_url":"",
        "Title":"",
        "menu_items":[
                {"item": 
                    [{"id":"home1","label":"Home" }]
                    },
                {"item":    
                    [{"id":"enrol1","label":"Enrollment" }]
                },
                {"item":
                   [{"id":"transcripts1","label":"Completed Courses"}]
                },
                {"item":
                   [{"id":"goals1","label":"Goals"}]
                },
                {"item":
                    [{"id":"rprojects1","label":"Reference Projects"}]
                },
                {"item":
                      [{"id":"iwh1","label":"Internal Work History"}]
                },
                {"item":
                     [{"id":"ewh1","label":"EXternal Work History"}]
                }
                ]
        },{.....}
 ]
 }

i need to parse the data and get the values of id,label ,i write some code to parse the data but it didnt work. 我需要解析数据并获取id,label的值,我编写了一些代码来解析数据,但是没有用。

  JSONObject subObj = new JSONObject(data2);
  JSONObject innerObj1 = subObj.getJSONObject("result");
  JSONObject subArrayObj = innerObj1.getJSONObject("menu_items");
  for(int j =0;j < subArrayObj.length();j++) {
  JSONObject innsersubObj = subArrayObj.getJSONObject("item");
  String id = innsersubObj.getString("id");
  String label = innsersubObj.getString("label");
  Log.e("id",id);
  Log.e("label",label);
   }

How to parse the data any thing need to change in the code ? 如何解析数据中任何需要更改的数据?

You have to use JSONObject and JSONArray are different objects, you have to use correct class: 您必须使用JSONObject和JSONArray是不同的对象,您必须使用正确的类:

JSONArray resultArray = subObj.getJSONArray("result");
JSONObject firstItem = resultArray.getJSONObject(0);
JSONArray menuItems = firstItem.getJSONArray("menu_items");

etc. 等等

JSONARRAY subArrayObj = innerObj1.getJSONARRAY("menu_items"); JSONARRAY subArrayObj = innerObj1.getJSONARRAY(“ menu_items”);

Since menu_items is returning an array..it should be collected in an array object.. 由于menu_items返回数组,因此应将其收集在数组对象中。

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

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