-1

I have this Json file and would like to have help in parsing them and setting the Country,State and City list to my autocomplete Text views in android . I have separate feilds for them. Any help? { "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" ] } ] } ] }

Edit 1 :See this link for actual file https://github.com/sagarshirbhate/Country-State-City-Database/blob/master/Contries.json I want to have 3 separate arrays of strings from the above code having Country names , State names for each country , and then City names using the country and State name.

Edit 2:I am not understanding how can I parse the models. I want to parse them for auto complete text boxes

  • Actually I want to know how can Get an Array of country names , city names and state names as 3 separate arrays of strings from the above json. Furthe see this link for the actual file https://github.com/sagarshirbhate/Country-State-City-Database/blob/master/Contries.json – Mohsin Ahamed Jan 01 '18 at 18:42
  • @MohsinAhamed take your JSON response into the method and manually parse it out! like I showed in the answer! or else Mr.Pavneet_Singh is right to use GSON that's also a nice solution – Rizwan Atta Jan 01 '18 at 18:49

2 Answers2

1

You can use Gson that is the most common solution within android development.

1

here you go! if there is the model class for getter and setters :-) this chunk of code will let you get started easily. try it! and lemme know if you want a model for it too! its easy! i will try to Edit again! actually! same you have to do for inner arrays of yours!

public void parsetheJson(String your_string_of_json){
     try {
            JSONArray mCountriesArray = new JSONArray(your_string_of_json);
            ArrayList<CountryDataModel> mCountryArray = new ArrayList<CountryDataModel>;
            for(int i = 0;i<mCountriesArray.length();i++){
                mCountryArray.add(mCountriesArray.getJSONObject(i));
         //this will give you an arraylist of all JSONobjects
         // then you have to do the same thing for the inner arrays
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
Rizwan Atta
  • 3,222
  • 2
  • 19
  • 31
  • thanks @rizwan , I have decided to clean this JSON so that it will be more easy to parse rather than actually coding it. – Mohsin Ahamed Jan 01 '18 at 18:55
  • yeah that would be great but there are some usefull libraries for this like json or GSON! you should also look for those too. but obviously making your JSON response less complicated is quite nice solution. #happy_hunting @MohsinAhamed – Rizwan Atta Jan 01 '18 at 19:13