您当前的位置: 首页 >  cmmboy1990 json

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

cmmboy1990 发布时间:2022-05-24 10:22:32 ,浏览量:9

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();
    }
关注
打赏
1688896170
查看更多评论

cmmboy1990

暂无认证

  • 9浏览

    0关注

    131博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.0791s