使用动态JSON对象名称进行JSON解析?

时间:2014-12-09 11:50:24

标签: android json jsonobject

我知道JSON解析,但在我当前的项目中,我得到了一些不同类型的JSON格式,其中所有对象的名称都是动态的,并且它们可能会发生变化。

{
    "category_id": "1",
    "parent_id": "0",
    "name": "Root Catalog",
    "is_active": null,
    "position": "0",
    "level": "0",
    "children": {
        "0": {
            "category_id": "2",
            "parent_id": "1",
            "name": "Default Category",
            "is_active": "1",
            "position": "1",
            "level": "1",
            "children": {
                "0": {
                    "category_id": "32",
                    "parent_id": "2",
                    "name": "Services",
                    "is_active": "1",
                    "position": "1",
                    "level": "2",
                    "children": {
                        "0": {
                            "category_id": "20",
                            "parent_id": "32",
                            "name": "Makeup",
                            "is_active": "1",
                            "position": "1",
                            "level": "3",
                            "children": {
                                "0": {
                                    "category_id": "22",
                                    "parent_id": "20",
                                    "name": "Bridal Makeup",
                                    "is_active": "1",
                                    "position": "1",
                                    "level": "4",
                                    "children": {

                                    }
                                },
                                "1": {
                                    "category_id": "23",
                                    "parent_id": "20",
                                    "name": "General Makeup",
                                    "is_active": "1",
                                    "position": "2",
                                    "level": "4",
                                    "children": {

                                    }
                                }
                            }
                        },
                        "1": {
                            "category_id": "21",
                            "parent_id": "32",
                            "name": "Skin Services",
                            "is_active": "1",
                            "position": "2",
                            "level": "3",
                            "children": {
                                "0": {
                                    "category_id": "24",
                                    "parent_id": "21",
                                    "name": "Bleach",
                                    "is_active": "1",
                                    "position": "1",
                                    "level": "4",
                                    "children": {

                                    }
                                },
                                "1": {
                                    "category_id": "25",
                                    "parent_id": "21",
                                    "name": "Cleanup",
                                    "is_active": "1",
                                    "position": "2",
                                    "level": "4",
                                    "children": {

                                    }
                                },
                                "2": {
                                    "category_id": "26",
                                    "parent_id": "21",
                                    "name": "General Facials",
                                    "is_active": "1",
                                    "position": "3",
                                    "level": "4",
                                    "children": {

                                    }
                                },
                                "3": {
                                    "category_id": "27",
                                    "parent_id": "21",
                                    "name": "Masks",
                                    "is_active": "1",
                                    "position": "4",
                                    "level": "4",
                                    "children": {

                                    }
                                },
                                "4": {
                                    "category_id": "28",
                                    "parent_id": "21",
                                    "name": "Special Facials",
                                    "is_active": "1",
                                    "position": "5",
                                    "level": "4",
                                    "children": {

                                    }
                                },
                                "5": {
                                    "category_id": "29",
                                    "parent_id": "21",
                                    "name": "Threading",
                                    "is_active": "1",
                                    "position": "6",
                                    "level": "4",
                                    "children": {

                                    }
                                },
                                "6": {
                                    "category_id": "30",
                                    "parent_id": "21",
                                    "name": "Waxing",
                                    "is_active": "1",
                                    "position": "7",
                                    "level": "4",
                                    "children": {

                                    }
                                },
                                "7": {
                                    "category_id": "38",
                                    "parent_id": "21",
                                    "name": "Special Services",
                                    "is_active": "1",
                                    "position": "8",
                                    "level": "4",
                                    "children": {

                                    }
                                },
                                "8": {
                                    "category_id": "40",
                                    "parent_id": "21",
                                    "name": "Lip Care",
                                    "is_active": "1",
                                    "position": "9",
                                    "level": "4",
                                    "children": {

                                    }
                                },
                                "9": {
                                    "category_id": "41",
                                    "parent_id": "21",
                                    "name": "Eye Care",
                                    "is_active": "1",
                                    "position": "10",
                                    "level": "4",
                                    "children": {

                                    }
                                }
                            }
                        },
                        "2": {
                            "category_id": "3",
                            "parent_id": "32",
                            "name": "Body Services",
                            "is_active": "1",
                            "position": "3",
                            "level": "3",
                            "children": {
                                "0": {
                                    "category_id": "4",
                                    "parent_id": "3",
                                    "name": "Body Wraps",
                                    "is_active": "1",
                                    "position": "1",
                                    "level": "4",
                                    "children": {

                                    }
                                },
                                "1": {
                                    "category_id": "5",
                                    "parent_id": "3",
                                    "name": "Reflexology",
                                    "is_active": "1",
                                    "position": "2",
                                    "level": "4",
                                    "children": {

                                    }
                                },
                                "2": {
                                    "category_id": "6",
                                    "parent_id": "3",
                                    "name": "Special Body Services",
                                    "is_active": "1",
                                    "position": "3",
                                    "level": "4",
                                    "children": {

                                    }
                                }
                            }
                        },
                        "3": {
                            "category_id": "7",
                            "parent_id": "32",
                            "name": "Hair Services",
                            "is_active": "1",
                            "position": "4",
                            "level": "3",
                            "children": {
                                "0": {
                                    "category_id": "8",
                                    "parent_id": "7",
                                    "name": "Chemical Treatments",
                                    "is_active": "0",
                                    "position": "1",
                                    "level": "4",
                                    "children": {

                                    }
                                },
                                "1": {
                                    "category_id": "42",
                                    "parent_id": "7",
                                    "name": "Hair Extensions",
                                    "is_active": "1",
                                    "position": "2",
                                    "level": "4",
                                    "children": {

                                    }
                                },
                                "2": {
                                    "category_id": "9",
                                    "parent_id": "7",
                                    "name": "Hair Color",
                                    "is_active": "1",
                                    "position": "3",
                                    "level": "4",
                                    "children": {

                                    }
                                },
                                "3": {
                                    "category_id": "10",
                                    "parent_id": "7",
                                    "name": "Haircut",
                                    "is_active": "1",
                                    "position": "4",
                                    "level": "4",
                                    "children": {

                                    }
                                },
                                "4": {
                                    "category_id": "11",
                                    "parent_id": "7",
                                    "name": "Hair Straightening",
                                    "is_active": "1",
                                    "position": "5",
                                    "level": "4",
                                    "children": {

                                    }
                                },
                                "5": {
                                    "category_id": "12",
                                    "parent_id": "7",
                                    "name": "Hair Styling",
                                    "is_active": "1",
                                    "position": "6",
                                    "level": "4",
                                    "children": {

                                    }
                                },
                                "6": {
                                    "category_id": "13",
                                    "parent_id": "7",
                                    "name": "Hair Treatments",
                                    "is_active": "1",
                                    "position": "7",
                                    "level": "4",
                                    "children": {

                                    }
                                },
                                "7": {
                                    "category_id": "14",
                                    "parent_id": "7",
                                    "name": "Other Hair Services",
                                    "is_active": "1",
                                    "position": "8",
                                    "level": "4",
                                    "children": {

                                    }
                                },
                                "8": {
                                    "category_id": "15",
                                    "parent_id": "7",
                                    "name": "Special Hair Services",
                                    "is_active": "1",
                                    "position": "9",
                                    "level": "4",
                                    "children": {

                                    }
                                }
                            }
                        },
                        "4": {
                            "category_id": "16",
                            "parent_id": "32",
                            "name": "Hands And Feet",
                            "is_active": "1",
                            "position": "5",
                            "level": "3",
                            "children": {
                                "0": {
                                    "category_id": "17",
                                    "parent_id": "16",
                                    "name": "Manicure",
                                    "is_active": "1",
                                    "position": "1",
                                    "level": "4",
                                    "children": {

                                    }
                                },
                                "1": {
                                    "category_id": "18",
                                    "parent_id": "16",
                                    "name": "Nail Art",
                                    "is_active": "1",
                                    "position": "2",
                                    "level": "4",
                                    "children": {

                                    }
                                },
                                "2": {
                                    "category_id": "19",
                                    "parent_id": "16",
                                    "name": "Pedicure",
                                    "is_active": "1",
                                    "position": "3",
                                    "level": "4",
                                    "children": {

                                    }
                                }
                            }
                        }
                    }
                },
                "1": {
                    "category_id": "33",
                    "parent_id": "2",
                    "name": "Memberships",
                    "is_active": "1",
                    "position": "2",
                    "level": "2",
                    "children": {

                    }
                },
                "2": {
                    "category_id": "34",
                    "parent_id": "2",
                    "name": "Packages",
                    "is_active": "1",
                    "position": "3",
                    "level": "2",
                    "children": {

                    }
                },
                "3": {
                    "category_id": "35",
                    "parent_id": "2",
                    "name": "Offers",
                    "is_active": "1",
                    "position": "4",
                    "level": "2",
                    "children": {

                    }
                },
                "4": {
                    "category_id": "36",
                    "parent_id": "2",
                    "name": "Customer Reviews",
                    "is_active": "1",
                    "position": "5",
                    "level": "2",
                    "children": {

                    }
                },
                "5": {
                    "category_id": "37",
                    "parent_id": "2",
                    "name": "Gallery",
                    "is_active": "1",
                    "position": "6",
                    "level": "2",
                    "children": {

                    }
                },
                "6": {
                    "category_id": "31",
                    "parent_id": "2",
                    "name": "Contact us",
                    "is_active": "1",
                    "position": "7",
                    "level": "2",
                    "children": {

                    }
                }
            }
        }
    }
}

所以在doInBackground()中,我的表现如下: -

@Override
protected String doInBackground(String... params) {

    ServiceHandler serviceHandler = new ServiceHandler();
    String jsonStr = serviceHandler.makeServiceCall(
            Constants.SIDE_DRAWER_SERVICES, ServiceHandler.GET);
    try {
        JSONObject jobj = new JSONObject(jsonStr);
        Log.d("", "" + jsonStr.toString());
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return jsonStr;
}

in String" jsonStr"我得到了完整的数据作为响应,但是当它转换为JSONObject时,就像" JSONObject jobj = new JSONObject(jsonStr);"我错过了这样的数据: -

{
    "position": "0",
    "children": {
        "0": {
            "position": "1",
            "children": {
                "3": {
                    "position": "4",
                    "children": {

                    },
                    "category_id": "35",
                    "level": "2",
                    "is_active": "1",
                    "name": "Offers",
                    "parent_id": "2"
                },
                "2": {
                    "position": "3",
                    "children": {

                    },
                    "category_id": "34",
                    "level": "2",
                    "is_active": "1",
                    "name": "Packages",
                    "parent_id": "2"
                },
                "1": {
                    "position": "2",
                    "children": {

                    },
                    "category_id": "33",
                    "level": "2",
                    "is_active": "1",
                    "name": "Memberships",
                    "parent_id": "2"
                },
                "0": {
                    "position": "1",
                    "children": {
                        "3": {
                            "position": "4",
                            "children": {
                                "3": {
                                    "position": "4",
                                    "children": {

                                    },
                                    "category_id": "10",
                                    "level": "4",
                                    "is_active": "1",
                                    "name": "Haircut",
                                    "parent_id": "7"
                                },
                                "2": {
                                    "position": "3",
                                    "children": {

                                    },
                                    "category_id": "9",
                                    "level": "4",
                                    "is_active": "1",
                                    "name": "Hair Color",
                                    "parent_id": "7"
                                },
                                "1": {
                                    "position": "2",
                                    "children": {

                                    },
                                    "category_id": "42",
                                    "level": "4",
                                    "is_active": "1",
                                    "name": "Hair Extensions",
                                    "parent_id": "7"
                                },
                                "0": {
                                    "position": "1",
                                    "children": {

                                    },
                                    "category_id": "8",
                                    "level": "4",
                                    "is_active": "0",
                                    "name": "Chemical Treatments",
                                    "parent_id": "7"
                                },
                                "7": {
                                    "position": "8",
                                    "children": {

                                    },
                                    "category_id": "14",
                                    "level": "4",
                                    "is_active": "1",
                                    "name": "Other Hair Services",
                                    "parent_id": "7"
                                },
                                "6": {
                                    "position": "7",
                                    "children": {

                                    },
                                    "category_id": "13",
                                    "level": "4",
                                    "is_active": "1",
                                    "name": "Hair Treatments",
                                    "parent_id": "7"
                                },
                                "5": {
                                    "position": "6",
                                    "children": {

                                    },
                                    "category_id": "12",
                                    "level": "4",
                                    "is_active": "1",
                                    "name": "Hair Styling",
                                    "parent_id": "7"
                                },
                                "4": {
                                    "position": "5",
                                    "children": {

                                    },
                                    "category_id": "11",
                                    "level": "4",
                                    "is_active": "1",
                                    "name": "Hair Straightening",
                                    "parent_id": "7"
                                },
                                "8": {
                                    "position": "9",
                                    "children": {

                                    },
                                    "category_id": "15",
                                    "level": "4",
                                    "is_active": "1",
                                    "name": "Special Hair Services",
                                    "parent_id": "7"
                                }
                            },
                            "category_id": "7",
                            "level": "3",
                            "is_active": "1",
                            "name": "Hair Services",
                            "parent_id": "32"
                        },
                        "2": {
                            "position": "3",
                            "children": {
                                "2": {
                                    "position": "3",
                                    "children": {

                                    },
                                    "category_id": "6",
                                    "level": "4",
                                    "is_active": "1",
                                    "name": "Special Body Services",
                                    "parent_id": "3"
                                },
                                "1": {
                                    "position": "2",
                                    "children": {

                                    },

我不知道我的JSON发生了什么。请帮帮我,告诉我JSON是否无效。

3 个答案:

答案 0 :(得分:3)

最后得到了解决方案,在调试时,字符串显示所有数据并不重要,但在JSONObject中,所有数据都可用但在调试时没有显示。当检索到时来自JSONObject的数据我获得了调试器中未显示的所有字段。

答案 1 :(得分:1)

要解析的示例JSON文件

[
    {

        "Language": "Afrikaans",
        "Native_Language": "Afrikaans",
        "code": "af"
    },

    {
        "Language": "Somali",
        "Native_Language": "af Soomaali",
        "code": "so"
    },
    {
        "Language": "Arabic",
        "Native_Language": "(al arabiya) العربية",
        "code": "ar"
    },
    {
        "Language": "Azerbaijani",
        "Native_Language": "Azərbaycan",
        "code": "az"
    }
]

以及用于解析上述JSON文件的Android代码。这项工作根据您的json响应完美地更改它。

package com.example.jsonparsing;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;

import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;


public class MainActivity extends ActionBarActivity {

    private ListView listViewLanguages;
    ArrayList<String> languageArray  = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listViewLanguages =(ListView)findViewById(R.id.languages);
        //===========================Reading Json File===================================//
        StringBuffer buffer = new StringBuffer();
        BufferedReader reader;
        try {
            reader = new BufferedReader(new InputStreamReader(getAssets().open("languagesupdated.json")));
            String readData;
            while((readData = reader.readLine()) != null)
            {
                buffer.append(readData);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        //===========================Getting Json Element===================================//
        String languageName;
        JSONArray array = null;
        try {
            array = new JSONArray(buffer.toString());
            for (int i = 0; i < array.length(); i++) {
                JSONObject jsonObject  = array.getJSONObject(i);
                languageName = jsonObject.getString("Language");
                languageArray.add(languageName);

                }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        //===========================GettingListView===================================//
          ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, languageArray);
          //listViewLanguages.setBackgroundColor(new ColorDrawable));
          listViewLanguages.setAdapter(adapter);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

答案 2 :(得分:0)

您的logcat仅显示一些数据行,因此请使用此功能打印数据。

通过longInfo(your_big_string);

进行调用
public static void longInfo(String str) {
    if(str.length() > 4000) {
        Log.i(TAG, str.substring(0, 4000));
        longInfo(str.substring(4000));
    } else
        Log.i(TAG, str);
}