1.通过获取笨的json文件进行解析,提示无法找到文件
List<QuestionBean> data = new Gson().fromJson(getStringFromAssert(NormalQuestionActivity.this, "data.json"), new TypeToken<List<QuestionBean>>() {
}.getType());
public static String getStringFromAssert(Context context, String fileName) {
try {
InputStream in = context.getAssets().open(fileName);
int length = in.available();
byte[] buffer = new byte[length];
in.read(buffer);
return new String(buffer, 0, buffer.length, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
2.需要将assets路径与src路径在同一级别,因为之前assets文件夹路径在src路径里面所以无法找到文件

本文介绍了一种从Android资源文件中加载并解析JSON数据的方法,并解决了因文件路径配置不当导致的读取失败问题。

1314

被折叠的 条评论
为什么被折叠?



