Android的存储嵌套的JSON对象的ArrayList多ExpandableListview [英] Android Storing Nested JSON Object to ArrayList MultiLevel ExpandableListview

查看:356
本文介绍了Android的存储嵌套的JSON对象的ArrayList多ExpandableListview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个嵌套的JSON数据,多层次expandablelistview。该JSON数据是这样的:

  [
    {
        DivisionID:2c0e9dc1-a6a7,
        DivisionName:Tyuio,
        细分:[
            {
                SubDivisionID:70c3ac53-eec6,
                SubDivisionName:FM2222,
                车辆:[
                    {
                        诺卜醇:00571564,
                        LASTUPDATE:2010年10月10日上午10点10分
                        LastSpeed​​:0,
                        LastLon:106.82176
                        位置:KNOWHERE
                    },
                    {
                        诺卜醇:352848020936627,
                        LASTUPDATE:2010年10月10日上午10点10分
                        LastSpeed​​:0,
                        LastLon:10124.228
                        位置:KNOWHERE2
                    }
                ]
            }
        ]
    }
]

这是我如何解析JSON数据,并且正在(编辑)

 保护的ArrayList<产品与GT; doInBackground(字符串参数... args){
//大厦参数清单<&的NameValuePair GT; PARAMS =新的ArrayList<&的NameValuePair GT;();SH的ServiceHandler =新的ServiceHandler();
//制作到URL的请求和响应得到
字符串jsonStr = sh.makeServiceCall(URL_DIVI,ServiceHandler.GET);尝试{
    迪维=新JSONArray(jsonStr)    pProductArrayList =新的ArrayList<产品及GT;();    如果(迪维!= NULL){
        //通过所有的数据循环
        的for(int i = 0; I< D​​ivi.length();我++){            JSONObject的C = Divi.getJSONObject(I)
            //存储在变量中的每个JSON项值
            字符串DIVID = c.getString(DIV_ID);
            字符串divname = c.getString(DIV_NAME);
            pProductArrayList.add(DIV_NAME.getJSONObject(ⅰ)的ToString());            如果(c.getString(DIV_NAME)。长度()!= 0)
            {
                Log.d(检查,装);
            }其他{
                Log.d(检查,空缺);
            }            pSubItemArrayList =新的ArrayList<&子类别GT;();
            SDIV =新JSONArray();
            如果(SDIV!= NULL){
                JSONArray SubDiv = c.getJSONArray(分区);
                对于(INT J = 0; J< SubDiv.length(); J ++)
                {
                    JSONObject的子= SubDiv.getJSONObject(J);
                    字符串subdivid = sub.getString(SUBDIV_ID);
                    字符串subdivname = sub.getString(SUBDIV_NAME);
                    pSubItemArrayList.add(新的子类别(SUBDIV_NAME,mItemListArray));
                    如果(sub.getString(SUBDIV_NAME)。长度()!= 0)
                    {
                        Log.d(CHECK2,subdvi_name填充);
                    }其他{
                        Log.d(CHECK2,subdvi_name空缺);
                    }
                    ArrayList的<&ITEMLIST GT; mItemListArray =新的ArrayList<&ITEMLIST GT;();                    Vehic =新JSONArray();
                    如果(Vehic!= NULL){
                        JSONArray Vehc = sub.getJSONArray(车辆);
                        对于(INT K = 0; K< Vehc.length(); K ++)
                        {
                            的JSONObject辆= Vehc.getJSONObject(K);
                            字符串诺卜醇= veh.getString(TAG_NOPOL);
                            字符串位置= veh.getString(TAG_LOCATION);
                            字符串longl = veh.getString(TAG_LONG);
                            mItemListArray.add(新ITEMLIST(TAG_NOPOL,TAG_LOCATION,TAG_LONG));
                            如果(veh.getString(TAG_NOPOL)。长度()!= 0)
                            {
                                Log.d(Check3,诺卜醇填充);
                            }其他{
                                Log.d(Check3,诺卜醇空缺);
                            }
                        //创建新的HashMap
                        HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>();                        //将每个子节点的HashMap键=>值
                        map.put(DIV_ID,DIVID);
                        map.put(DIV_NAME,divname);
                        map.put(SUBDIV_ID,subdivid);
                        map.put(SUBDIV_NAME,subdivname);
                        map.put(TAG_NOPOL,诺卜醇);
                        map.put(TAG_LOCATION,位置);
                        map.put(TAG_LONG,longl);                        //添加HashList到ArrayList的
                        diviList.add(地图);

我是尽量保存成JSON此ArrayList

 私人的ArrayList<产品与GT; pProductArrayList;
私人的ArrayList<&子类别GT; pSubItemArrayList;私有静态最后弦乐DIV_NAME =DivisionName;
私有静态最后弦乐SUBDIV_NAME =SubDivisionName;
私有静态最后弦乐TAG_NOPOL =诺卜醇;
私有静态最后弦乐TAG_LOCATION =位置;
私有静态最后弦乐TAG_LONG =LastLon;保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_home);    ArrayList的<&ITEMLIST GT; mItemListArray =新的ArrayList<&ITEMLIST GT;();
    mItemListArray.add(新ITEMLIST(TAG_NOPOL,TAG_LOCATION,TAG_LONG));    pSubItemArrayList =新的ArrayList<&子类别GT;();
    pSubItemArrayList.add(新的子类别(SUBDIV_NAME,mItemListArray));    pProductArrayList =新的ArrayList<产品及GT;();
    pProductArrayList.add(新产品(DIV_NAME,pSubItemArrayList));

所需的输出:

  | Tyuio | <  - 父列表
     | FM222 | - 子列表
          | 00571564 | < - 孙子列表#1
          | KNOWHERE |
          | 106.82176 |
          | 35284802 | < - 孙子列表#2
          | KNOWHERE2 |
          | 10124.228 |

我具有存储和显示数据到ArrayList中的一个问题。我不明白DivisionName(父表),SubdivisionName(儿童名单),以及所有车辆阵列(孙子列表)。这JSON数据是动态的。我不能找出如何解决这个问题。我看过很多网上教程,但这些不适合我很有帮助。如果我没有在我的问题道歉很清楚,只是问我会澄清。谢谢提前


解决方案

 类产品{
    串DIVID;
    串divname;
    ArrayList的<&子类别GT;子类别;
}一流的子类别{
    串SubDivisionID;
    串SubDivisionName;
    ArrayList的<汽车与GT;汽车;
}类车辆{
    串诺卜醇;
    串LASTUPDATE;
    串LastSpeed​​;
    串LastLon;
    字符串位置;
}私人的ArrayList<产品与GT;解析(字符串jsonStr){
    // TODO自动生成方法存根
    尝试{
        JSONArray迪维=新JSONArray(jsonStr);        ArrayList的<产品与GT; pProductArrayList =新的ArrayList<产品及GT;();        如果(迪维!= NULL){
            //通过所有的数据循环
            的for(int i = 0; I< D​​ivi.length();我++){                JSONObject的C = Divi.getJSONObject(I)
                //存储在变量中的每个JSON项值
                产品产品=新产品();
                product.divid = c.getString(DivisionID);
                product.divname = c.getString(DivisionName);                JSONArray SDIV = c.getJSONArray(分区);                ArrayList的<&子类别GT; pSubItemArrayList =新的ArrayList<&子类别GT;();                对于(INT J = 0; J< SDiv.length(); J ++){
                    JSONObject的子= SDiv.getJSONObject(J);
                    子类别子类别=新的子类别();
                    subCategory.SubDivisionID =子
                            .getString(SubDivisionID);
                    subCategory.SubDivisionName =子
                            .getString(SubDivisionName);
                    JSONArray Vehc = sub.getJSONArray(车辆);                    ArrayList的<汽车与GT; mItemListArray =新的ArrayList<车辆与GT;();                    对于(INT K = 0; K< Vehc.length(); K ++){
                        的JSONObject辆= Vehc.getJSONObject(K);
                        车辆车辆=新车();
                        vehicle.Nopol = veh.getString(诺卜醇);
                        vehicle.Location = veh.getString(位置);
                        vehicle.LastLon = veh.getString(LastLon);
                        vehicle.LastSpeed​​ = veh.getString(LastSpeed​​);
                        vehicle.LastUpdate = veh.getString(LASTUPDATE);
                        mItemListArray.add(车辆);                    }
                    subCategory.Vehicles = mItemListArray;
                    pSubItemArrayList.add(子类别);
                }
                product.subCategories = pSubItemArrayList;
                pProductArrayList.add(产品);
            }        }
        返回pProductArrayList;    }赶上(例外五){
        // TODO:处理异常
    }
    返回null;
}

我HPOE这个code会帮助你。

I have a nested json data and multilevel expandablelistview. The JSON data is looks like this :

[
    {
        "DivisionID": "2c0e9dc1-a6a7",
        "DivisionName": "Tyuio",
        "SubDivision": [
            {
                "SubDivisionID": "70c3ac53-eec6",
                "SubDivisionName": "FM2222",
                "Vehicle": [
                    {
                        "Nopol": "00571564",
                        "LastUpdate": "Oct 10 2010 10:10AM",
                        "LastSpeed": 0,
                        "LastLon": 106.82176
                        "Location": "KNOWHERE"
                    },
                    { 
                        "Nopol": "352848020936627",
                        "LastUpdate": "Oct 10 2010 10:10AM",
                        "LastSpeed": 0,
                        "LastLon": 10124.228
                        "Location": "KNOWHERE2"
                    }
                ]
            }
        ]
    }
]

this is how i parsing the json data and is working (EDIT):

protected ArrayList<Product> doInBackground(String... args) {
// Building Parameters

List<NameValuePair> params = new ArrayList<NameValuePair>();

ServiceHandler sh = new ServiceHandler();   
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(URL_DIVI , ServiceHandler.GET);

try { 
    Divi = new JSONArray(jsonStr)

    pProductArrayList=new ArrayList<Product>();

    if (Divi != null) {
        // looping through All data
        for (int i = 0; i < Divi.length(); i++) {

            JSONObject c = Divi.getJSONObject(i);
            // Storing each json item values in variable
            String divid = c.getString(DIV_ID);
            String divname = c.getString(DIV_NAME);
            pProductArrayList.add(DIV_NAME.getJSONObject(i).toString());

            if (c.getString(DIV_NAME).length() != 0)
            {
                Log.d("Check", "filled");
            }else{
                Log.d("Check", "unfilled");
            }

            pSubItemArrayList=new ArrayList<SubCategory>();
            SDiv = new JSONArray();
            if (SDiv != null) {
                JSONArray SubDiv = c.getJSONArray("SubDivision");
                for (int j=0; j<SubDiv.length(); j++)
                {
                    JSONObject sub = SubDiv.getJSONObject(j);
                    String subdivid = sub.getString(SUBDIV_ID);
                    String subdivname = sub.getString(SUBDIV_NAME);
                    pSubItemArrayList.add(new SubCategory(SUBDIV_NAME, mItemListArray)); 
                    if (sub.getString(SUBDIV_NAME).length() != 0)
                    {
                        Log.d("Check2", "subdvi_name filled");
                    }else{
                        Log.d("Check2", "subdvi_name unfilled");
                    }
                    ArrayList<ItemList> mItemListArray=new ArrayList<ItemList>();

                    Vehic = new JSONArray();
                    if (Vehic != null) {
                        JSONArray Vehc = sub.getJSONArray("Vehicle");
                        for (int k=0; k<Vehc.length(); k++) 
                        {
                            JSONObject veh = Vehc.getJSONObject(k);
                            String nopol = veh.getString(TAG_NOPOL);
                            String location = veh.getString(TAG_LOCATION);
                            String longl = veh.getString(TAG_LONG);
                            mItemListArray.add(new ItemList(TAG_NOPOL,TAG_LOCATION,TAG_LONG));
                            if (veh.getString(TAG_NOPOL).length() != 0)
                            {
                                Log.d("Check3", "nopol filled");
                            }else{
                                Log.d("Check3", "nopol unfilled");
                            }
                        // creating new HashMap
                        HashMap<String, String> map = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        map.put(DIV_ID, divid);
                        map.put(DIV_NAME, divname);
                        map.put(SUBDIV_ID, subdivid);
                        map.put(SUBDIV_NAME, subdivname);
                        map.put(TAG_NOPOL, nopol);
                        map.put(TAG_LOCATION, location);
                        map.put(TAG_LONG, longl);

                        // adding HashList to ArrayList
                        diviList.add(map);

i am try to storing JSON into this ArrayList

private ArrayList<Product>pProductArrayList;
private ArrayList<SubCategory>pSubItemArrayList;

private static final String DIV_NAME= "DivisionName";
private static final String SUBDIV_NAME = "SubDivisionName";
private static final String TAG_NOPOL = "Nopol";
private static final String TAG_LOCATION = "Location";
private static final String TAG_LONG = "LastLon";

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    ArrayList<ItemList> mItemListArray=new ArrayList<ItemList>();
    mItemListArray.add(new ItemList(TAG_NOPOL, TAG_LOCATION,TAG_LONG));

    pSubItemArrayList=new ArrayList<SubCategory>();
    pSubItemArrayList.add(new SubCategory(SUBDIV_NAME, mItemListArray));    

    pProductArrayList=new ArrayList<Product>();
    pProductArrayList.add(new Product(DIV_NAME, pSubItemArrayList));

Desired output :

| Tyuio       |  <-- parent list
     |  FM222      |  -- child list
          |  00571564   |   <-- grandchild list #1
          |  KNOWHERE   |
          |  106.82176  |
          |  35284802   |   <-- grandchild list #2
          |  KNOWHERE2  |
          |  10124.228  |

I have a problem to store and show the data into ArrayList. I don't get DivisionName (parent-list), SubdivisionName (child-list), and all of vehicle array (grandchild-list). This JSON data is dynamic. I can't finding out how to solve this problem. I have seen many online tutorials but those was not helpful for me. If I'm not very clear in my question apologies, just ask and I'll clarify.. Thank you in advance

解决方案

class Product {
    String divid;
    String divname;
    ArrayList<SubCategory> subCategories;
}

class SubCategory {
    String SubDivisionID;
    String SubDivisionName;
    ArrayList<Vehicle> Vehicles;
}

class Vehicle {
    String Nopol;
    String LastUpdate;
    String LastSpeed;
    String LastLon;
    String Location;
}

private ArrayList<Product> parsing(String jsonStr) {
    // TODO Auto-generated method stub
    try {
        JSONArray Divi = new JSONArray(jsonStr);

        ArrayList<Product> pProductArrayList = new ArrayList<Product>();

        if (Divi != null) {
            // looping through All data
            for (int i = 0; i < Divi.length(); i++) {

                JSONObject c = Divi.getJSONObject(i);
                // Storing each json item values in variable
                Product product = new Product();
                product.divid = c.getString("DivisionID");
                product.divname = c.getString("DivisionName");

                JSONArray SDiv = c.getJSONArray("SubDivision");

                ArrayList<SubCategory> pSubItemArrayList = new ArrayList<SubCategory>();

                for (int j = 0; j < SDiv.length(); j++) {
                    JSONObject sub = SDiv.getJSONObject(j);
                    SubCategory subCategory = new SubCategory();
                    subCategory.SubDivisionID = sub
                            .getString("SubDivisionID");
                    subCategory.SubDivisionName = sub
                            .getString("SubDivisionName");


                    JSONArray Vehc = sub.getJSONArray("Vehicle");

                    ArrayList<Vehicle> mItemListArray = new ArrayList<Vehicle>();

                    for (int k = 0; k < Vehc.length(); k++) {
                        JSONObject veh = Vehc.getJSONObject(k);
                        Vehicle vehicle = new Vehicle();
                        vehicle.Nopol = veh.getString("Nopol");
                        vehicle.Location = veh.getString("Location");
                        vehicle.LastLon = veh.getString("LastLon");
                        vehicle.LastSpeed = veh.getString("LastSpeed");
                        vehicle.LastUpdate = veh.getString("LastUpdate");
                        mItemListArray.add(vehicle);

                    }
                    subCategory.Vehicles=mItemListArray;
                    pSubItemArrayList.add(subCategory);
                }
                product.subCategories = pSubItemArrayList;
                pProductArrayList.add(product);
            }

        }
        return pProductArrayList;

    } catch (Exception e) {
        // TODO: handle exception
    }
    return null;
}

i hpoe this code will help you.

这篇关于Android的存储嵌套的JSON对象的ArrayList多ExpandableListview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆