简体   繁体   English

如何正确解析此JSON?

[英]How do I properly parse this JSON?

I have a big JSON file that contains Countries, States and Cities. 我有一个包含国家,州和城市的大JSON文件。 The JSON looks like this: JSON如下所示:

{
"Countries":[
        {
        "CountryName":"India",
        "States":[
                    {
                    "StateName":"Maharashtra",
                    "Cities":[
                            "Pune",
                            "Nagpur",
                            "Mumbai"
                              ]
                     },
                     {
                     "StateName":"Kerala",
                     "Cities":[
                                "Kochi",
                                "Munnar"
                              ]
                      }
                   ]
         },
         {
          "CountryName":"Australia",
          "States":[
                    {
                    "StateName":"Aukland",
                    "Cities":[
                            "GlenField",
                            "Henderson",
                                "MilFord"
                              ]
                     },
                     {
                     "StateName":"Melbourne",
                     "Cities":[
                                "Melbourne",
                                "South Oakleigh"
                              ]
                      }
                   ]
          }
       ]
  }

I have an activity in my app that requires the user to select a country and upon selecting a country I want to get all the states of that country as an array list. 我的应用程序中有一个活动,要求用户选择一个国家,选择一个国家后,我想获取该国家的所有州作为数组列表。 And when a specific state is selected, I want to get all the cities of that state as an array list. 当选择了一个特定州时,我想将该州的所有城市作为数组列表。

I am able to get a list of all Countries using this method: 我可以使用此方法获取所有国家/地区的列表:

public void loadCountries(String parent, String child, ArrayList<String> listValue)
{
    try {
        JSONObject obj = new JSONObject(loadJSONFromAsset());
        JSONArray m_jArry = obj.getJSONArray(parent);;
        ArrayList<HashMap<String, String>> formList = new ArrayList<HashMap<String, String>>();
        HashMap<String, String> m_li;

      //  listValue = new ArrayList<>();


        for (int i = 0; i < m_jArry.length(); i++) {
            JSONObject jo_inside = m_jArry.getJSONObject(i);
            Log.d("Details-->", jo_inside.getString(child));
            listValue.add(jo_inside.getString(child));
        }


    } catch (JSONException e) {
        e.printStackTrace();
    }
}

public String loadJSONFromAsset() {
    String json = null;
    try {
        InputStream is = getAssets().open("Contries.json");
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        json = new String(buffer, "UTF-8");
    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    }
    return json;
}

Then, in the onCreate method, using this line gives me all the countries in an array list 然后,在onCreate方法中,使用此行将显示数组列表中的所有国家/地区

loadCountries("Countries", "CountryName", countries);

Since this is my first time working with a JSON file in AndroidStudio/Java, I have no idea what to do to get the states and the cities. 由于这是我第一次使用AndroidStudio / Java中的JSON文件,因此我不知道该如何获取州和城市。 Any help is greatly appreciated. 任何帮助是极大的赞赏。

You could use GSON library. 您可以使用GSON库。 Just create corresponding java object which will reflect json structure and do like here: 只需创建相应的java对象即可反映json结构,如下所示:

MyClass data = new Gson().fromJson(json, MyClass.class);

Respect for trying to do this yourself, but we live in the age of the API. 尊重自己尝试执行此操作,但是我们生活在API时代。 Check out Google's GSON library, it parses JSON to java objects, and java objects to JSON. 检出Google的GSON库,它将JSON解析为java对象,并将java对象解析为JSON。 It's one of the most useful APIs out there. 它是最有用的API之一。 https://github.com/google/gson https://github.com/google/gson

Try this: 尝试这个:

 Map<String, Map<String, Map<Integer, String>>> data = new HashMap<>();

              try {
                JSONObject object = new JSONObject(loadJSONFromAsset());
                JSONArray countries = object.getJSONArray("Countries");

                for (int i = 0; i < countries.length(); i++) {

                    JSONObject state = countries.getJSONObject(i);
                    JSONArray statesList = state.getJSONArray("States");
                    String countryName = countries.getJSONObject(i).getString("CountryName");

                    for (int j = 0; j < statesList.length(); j++) {

                        JSONObject city = statesList.getJSONObject(j);
                        JSONArray citiesList = city.getJSONArray("Cities");
                        String stateName = city.getString("StateName");

                        Map<String, Map<Integer, String>> stateHashMap = new HashMap<>();

                        for (int k = 0; k < citiesList.length(); k++) {

                            cityList.add(citiesList.getString(k));
                            String cityName = citiesList.getString(k);

                            Map<Integer, String> cityHashmap = new HashMap<>();
                            cityHashmap.put(k, cityName);
                            stateHashMap.put(stateName, cityHashmap);

                            data.put(countryName, stateHashMap);

                        }
                    }
                }


            } catch (JSONException e) {
                e.printStackTrace();
            }

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

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