1.核心代码
Gson gson = new Gson();//引入 implementation 'com.google.code.gson:gson:2.8.6'
List totalNewsDataList =
gson.fromJson(
CommonUtils.getStringFromJsonPath(this, "entry/resources/rawfile/news_datas.json"),
new TypeToken() { }.getType());
getStringFromJsonPath
public static String getStringFromJsonPath(Context context, String jsonPath) {
Resource datasResource;
try {
datasResource = context.getResourceManager().getRawFileEntry(jsonPath).openRawFile();
byte[] buffers = new byte[datasResource.available()];
if (datasResource.read(buffers) != -1) {
return new String(buffers, StandardCharsets.UTF_8);
}
} catch (IOException e) {
//LogUtils.error(TAG, ERROR_MESSAGE);
}
return Optional.of(jsonPath).toString();
}