【发布时间】:2018-03-24 01:34:12
【问题描述】:
我有一个包含JSONObject 数组的json 文件,在读取并将其转换为字符串后,我想创建一个Java 对象的ArrayList,这是我目前的代码:
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();
}
其中 CountryCode 是模型类,有 name 字段,countrycode 是 JSONObjects 的映射(每个 json 对象都有 name 和 countrycode)
出现错误:
ClassCastException: com.google.gson.internal.LinkedTreeMap 不能 转换为 com.myhealthahand.hah.objects.CountryCode
【问题讨论】:
-
提供更多细节,如 json 字符串和 CountryCode 类。
标签: java android json oop gson