小编Sau*_*Rai的帖子

如何使用 Kotlin 从 android 中的 asset 中读取 json 文件?

我已经学过 java,但发现 Kotlin 很难。

我已经用谷歌搜索过,但没有一个对我有用。

/**
 * Get the json data from json file.
 *
 * @param context  the context to acces the resources.
 * @param fileName the name of the json file
 * @return json as string
 */
public static String getJsonFromAsset(Context context, String fileName) {
    String json = "";
    try {
        InputStream stream = context.getAssets().open(fileName);
        int size = stream.available();
        byte[] buffer = new byte[size];
        stream.read(buffer);
        stream.close();
        json = new String(buffer, "UTF-8");

    } catch (Exception e) {
        e.printStackTrace();
    } …
Run Code Online (Sandbox Code Playgroud)

android json kotlin

5
推荐指数
1
解决办法
2万
查看次数

标签 统计

android ×1

json ×1

kotlin ×1