G
G
gamess4312021-11-20 13:30:48
Java
gamess431, 2021-11-20 13:30:48

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"
    }
]

Now the question is: how do I display in the ListView the values ​​of the key Name and Unicode and Dial?
My non-working class who do not know where and how to use!
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;
    }
}

my parser
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);

my adapter
adapter = new ArrayAdapter<>(
                this,
                android.R.layout.simple_list_item_1,
                getResources().getStringArray(exemple)); // here for country name !
        selectCountry.setAdapter(adapter);

I know that my code is not correct and please give a working example

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Roo, 2021-11-20
@xez

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 question

Ask a Question

731 491 924 answers to any question