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

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

问题描述

我有一个嵌套的 json 数据和多级可扩展列表视图.JSON 数据如下所示:

<预><代码>[{"DivisionID": "2c0e9dc1-a6a7","部门名称": "Tyuio",细分":[{"SubDivisionID": "70c3ac53-eec6","SubDivisionName": "FM2222",车辆": [{"Nopol": "00571564","LastUpdate": "2010 年 10 月 10 日上午 10:10",最后的速度":0,LastLon":106.82176地点":知道"},{"诺波尔": "352848020936627","LastUpdate": "2010 年 10 月 10 日上午 10:10",最后的速度":0,LastLon":10124.228地点":KNOWHERE2"}]}]}]

这是我解析 json 数据的方式并且正在运行 (编辑):

protected ArrayListdoInBackground(字符串...参数){//建筑参数列表params = new ArrayList();ServiceHandler sh = new ServiceHandler();//向 url 发出请求并得到响应String jsonStr = sh.makeServiceCall(URL_DIVI, ServiceHandler.GET);尝试 {Divi = 新的 JSONArray(jsonStr)pProductArrayList=new ArrayList();if (Divi != null) {//遍历所有数据for (int i = 0; i ();SDiv = 新的 JSONArray();如果(SDiv != null){JSONArray SubDiv = c.getJSONArray("SubDivision");for (int j=0; jmItemListArray=new ArrayList();车辆 = 新的 JSONArray();如果(车辆!= null){JSONArray Vehc = sub.getJSONArray("车辆");for (int k=0; kmap = new HashMap();//将每个子节点添加到 HashMap key =>价值map.put(DIV_ID,divid);map.put(DIV_NAME, divname);map.put(SUBDIV_ID,细分);map.put(SUBDIV_NAME, subdivname);map.put(TAG_NOPOL, nopol);map.put(TAG_LOCATION, location);map.put(TAG_LONG, longl);//将 HashList 添加到 ArrayListdiviList.add(map);

我正在尝试将 JSON 存储到这个 ArrayList 中

私有ArrayListpProductArrayList;私有 ArrayListpSubItemArrayList;private static final String DIV_NAME="DivisionName";private static final String SUBDIV_NAME = "SubDivisionName";私有静态最终字符串 TAG_NOPOL = "Nopol";私有静态最终字符串 TAG_LOCATION = "位置";private static final String TAG_LONG = "LastLon";protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_home);ArrayListmItemListArray=new ArrayList();mItemListArray.add(new ItemList(TAG_NOPOL, TAG_LOCATION,TAG_LONG));pSubItemArrayList=new ArrayList();pSubItemArrayList.add(new SubCategory(SUBDIV_NAME, mItemListArray));pProductArrayList=new ArrayList();pProductArrayList.add(新产品(DIV_NAME, pSubItemArrayList));

期望的输出:

<代码>|秋尾 |<-- 父列表|FM222 |-- 子列表|00571564 |<-- 孙子列表 #1|知道||106.82176 ||35284802 |<-- 孙子列表#2|知乎2 ||10124.228 |

我在将数据存储和显示到 ArrayList 时遇到问题.我没有得到 DivisionName(父列表)、SubdivisionName(子列表)和所有车辆阵列(孙列表).此 JSON 数据是动态的.我不知道如何解决这个问题.我看过很多在线教程,但这些对我没有帮助.如果我在我的问题中不是很清楚抱歉,请提问,我会澄清.. 在此先感谢您

解决方案

class Product {字符串分割;字符串 divname;ArrayList<子类别>子类别;}类子类别{String SubDivisionID;字符串细分名称;ArrayList<车辆>车辆;}类车辆{字符串 Nopol;字符串 LastUpdate;字符串 LastSpeed;字符串 LastLon;字符串位置;}私有 ArrayList<产品>解析(字符串 jsonStr){//TODO 自动生成的方法存根尝试 {JSONArray Divi = new JSONArray(jsonStr);ArrayList<产品>pProductArrayList = new ArrayList();if (Divi != null) {//遍历所有数据for (int i = 0; i pSubItemArrayList = new ArrayList();for (int j = 0; j mItemListArray = new ArrayList();for (int k = 0; k 

我希望此代码可以帮助您.

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 MultiLevel ExpandableListview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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