There is a file with json extension. In google found this method of converting:

String jsonText = "{\"name\":\"Мурзик\",\"color\":-16777216,\"age\":9}"; GsonBuilder builder = new GsonBuilder(); Gson gson = builder.create(); Cat murzik = gson.fromJson(jsonText, Cat.class); Log.i("GSON", "Имя: " + murzik.name + "\nВозраст: " + murzik.age); 

jsonText is registered in the class. I do not understand how to get data from another file that is stored in assets

  • Where are the assets? - Roman C
  • Actually, your question is not about Json and objects, but about reading files from assets into a string. See the solution in my answer) - YuriySPb

1 answer 1

Here is a method on the cotlin that reads a file from assets into a string by its name:

 fun readFromAssets(context: Context, filename: String): String { val reader = BufferedReader(InputStreamReader(context.assets.open(filename), "UTF-8")) // do reading, usually loop until end of file reading val sb = StringBuilder() var line: String? = reader.readLine() while (line != null) { sb.append(line) // process line line = reader.readLine() } reader.close() return sb.toString() }