> 文档中心 > 鸿蒙 加载本地rowfile文件中的 json 数据

鸿蒙 加载本地rowfile文件中的 json 数据

1.核心代码

   Gson gson = new Gson();//引入 implementation 'com.google.code.gson:gson:2.8.6' List<NewsInfo> totalNewsDataList =  gson.fromJson(   CommonUtils.getStringFromJsonPath(this, "entry/resources/rawfile/news_datas.json"),   new TypeToken<List<NewsInfo>>() { }.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();    }

开发者涨薪指南 鸿蒙 加载本地rowfile文件中的 json 数据 48位大咖的思考法则、工作方式、逻辑体系