Answer the question
In order to leave comments, you need to log in
Java how to parse array of objects and display value on ListView?
Hello everyone, I have a Json file located in the Assets directory, it contains a list of all countries and codes, capitals, etc.. )) look like this,
{
"Country": [
{
"Iso2": "BD",
"Name": "Bangladesh",
"Iso3": "BGD",
"Unicode": "",
"Dial": "880",
"Currency": "BDT",
"Capital": "Dhaka",
"Continent": "AS"
},
{
"Iso2": "BE",
"Name": "Belgium",
"Iso3": "BEL",
"Unicode": "",
"Dial": "32",
"Currency": "EUR",
"Capital": "Brussels",
"Continent": "EU"
}
]
public class ParseCountry {
private String name;
private String code;
private String unicode;
public ParseCountry(String key, String value, String unicodeISO){
this.name = key;
this.code = value;
this.code = unicodeISO;
}
}
Gson gson = new Gson();
InputStream jsonFile = null;
try {
jsonFile = getAssets().open("json/countryData.json");
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "Json file is not found", Toast.LENGTH_LONG).show();
}
Type listType = new TypeToken<List<String>>(){}.getType();
List<String> countryData = gson.fromJson(String.valueOf(jsonFile), listType);
adapter = new ArrayAdapter<>(
this,
android.R.layout.simple_list_item_1,
getResources().getStringArray(exemple)); // here for country name !
selectCountry.setAdapter(adapter);
Answer the question
In order to leave comments, you need to log in
Here, for example, you can generate a model from your Jason: https://www.jsonschema2pojo.org/
Nothing more complicated.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question