如何正确解析此JSON?

时间:2018-04-28 11:55:45

标签: java json android-studio arraylist

我有一个包含国家,州和城市的大型JSON文件。 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"
                              ]
                      }
                   ]
          }
       ]
  }

我的应用中有一项活动,要求用户选择一个国家/地区,在选择国家/地区时,我希望将该国家/地区的所有州状态作为数组列表。当选择特定状态时,我希望将该状态的所有城市作为数组列表。

我可以使用此方法获取所有国家/地区的列表:

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;
}

然后,在onCreate方法中,使用此行为我提供了数组列表中的所有国家/地区

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

由于这是我第一次使用AndroidStudio / Java中的JSON文件,我不知道如何处理州和城市。非常感谢任何帮助。

3 个答案:

答案 0 :(得分:2)

您可以使用GSON库。只需创建相应的java对象,它将反映json结构,并在此处执行:

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

答案 1 :(得分:1)

尊重自己尝试这样做,但我们生活在API的时代。查看Google的GSON库,它将JSON解析为java对象,将java对象解析为JSON。它是最有用的API之一。 https://github.com/google/gson

答案 2 :(得分:0)

试试这个:

 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();
            }