2

I am new to Kotlin and trying to understand how I can read and parse the .json file. Say, I have a file Test.json with some json array and need to read the array and stored the content in mutable list.

I tried searching the blog but could not find the answer. Thanks in advance.

Amol K
  • 165
  • 3
  • 12
  • 4
    Possible duplicate of [How to parse JSON in Kotlin?](https://stackoverflow.com/questions/41928803/how-to-parse-json-in-kotlin) – tkruse Nov 30 '17 at 03:18

2 Answers2

5

It depends, if you know the format of the JSON file maps to one of your Kotlin classes then you could use a library such as Jackson, Gson, Klaxon or Moshi to convert the contents of the file to an instance of this class.

Alternatively you could manually parse the JSON using Java's JSONObjects and work through the nested map of JsonObjects/Values.

hexkid
  • 574
  • 1
  • 4
  • 13
0

I personally use this Klaxon library to parse json file and use in in my android application.
which I think will do your work.

Add dependncy to your gradle.build file

compile 'com.beust:klaxon:0.30'
Shankar
  • 2,890
  • 3
  • 25
  • 40