Как прочитать файл JSON из ресурсов в Android с помощью Kotlin?Android

Форум для тех, кто программирует под Android
Anonymous
Как прочитать файл JSON из ресурсов в Android с помощью Kotlin?

Сообщение Anonymous »

Я уже закончил работу с Java, но с Kotlin мне сложно.

Я уже искал в Google, но ни один из них мне не помог.

Код: Выделить всё

/**
* 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();
}
return json;
}
Мне нужен этот код на Kotlin.

Подробнее здесь: https://stackoverflow.com/questions/569 ... ing-kotlin

Вернуться в «Android»