J'ai un fichier json qui a un tableau de JSONObject
, après l'avoir lu et converti en chaîne, je veux créer un ArrayList
d'objets Java, voici mon code jusqu'à présent:Comment faire deJson de Gson créer arraylilst d'objet
ArrayList<CountryCode> arrayList;
arrayList = new ArrayList<CountryCode>();
try {
InputStream ims = context.getResources().getAssets().open("json/" + "countryCodes.json");
reader = new InputStreamReader(ims);
int size = ims.available();
byte[] buffer = new byte[size];
ims.read(buffer);
ims.close();
json = new String(buffer, "UTF-8");
arrayList = gson.fromJson(json , ArrayList.class);
Log.i("countrycode", String.valueOf(arrayList.get(0).getCode()));
} catch (IOException e) {
e.printStackTrace();
}
où est CountryCode classe modèle a des champs pour le nom, countrycode, qui est une carte de JSONObjects (chaque objet JSON a le nom et countrycode)
Obtenir erreur:
ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to com.myhealthathand.hah.objects.CountryCode
fournir plus de détails comme json string et CountryCo de classe. –