Parse a JSON String in Kotlin
This post will discuss how to parse a JSON String in Kotlin.
Kotlin doesn’t provide any support for converting a JSON string to an object. This post provides an overview of some of the available alternatives to accomplish this.
1. Using Gson
Google’s Gson library can be used to convert a JSON string to an equivalent Java object. You can use the Gson#fromJson() function to facilitate this conversion. The following code demonstrates this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import com.google.gson.Gson class Person { var name: String? = null var age: Int? = null var student: Student? = null override fun toString(): String { return "Person(name=$name, age=$age, student=$student)" } } class Student { var studentId: String? = null var subjects: List<String>? = null override fun toString(): String { return "Student(studentId=$studentId, subjects=$subjects)" } } fun main() { val jsonString = "{\"name\":\"Peter Parker\",\"age\":22," + "\"student\":{\"studentId\":\"Jon_Snow_22\"," + "\"subjects\":[\"Maths\",\"Science\"]}}" println("JSON string is: $jsonString") val person = Gson().fromJson(jsonString, Person::class.java) println("Person object: $person") } |
Output:
JSON string is: {"name":"Peter Parker","age":22,"student":{"studentId":"Jon_Snow_22","subjects":["Maths","Science"]}}
Person object: Person(name=Peter Parker, age=22, student=Student(studentId=Jon_Snow_22, subjects=[Maths, Science]))
2. Using Jackson
If you prefer the Jackson library over Gson, you can use its ObjectMapper#readValue() function to convert the JSON string into the specified object. Here’s the sample code:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import org.codehaus.jackson.map.ObjectMapper class Person { var name: String? = null var age: Int? = null var student: Student? = null override fun toString(): String { return "Person(name=$name, age=$age, student=$student)" } } class Student { var studentId: String? = null var subjects: List<String>? = null override fun toString(): String { return "Student(studentId=$studentId, subjects=$subjects)" } } fun main() { val jsonString = "{\"name\":\"Peter Parker\",\"age\":22," + "\"student\":{\"studentId\":\"Jon_Snow_22\"," + "\"subjects\":[\"Maths\",\"Science\"]}}" println("JSON string is: $jsonString") val person = ObjectMapper().readValue(jsonString, Person::class.java) println("Person object: $person") } |
Output:
JSON string is: {"name":"Peter Parker","age":22,"student":{"studentId":"Jon_Snow_22","subjects":["Maths","Science"]}}
Person object: Person(name=Peter Parker, age=22, student=Student(studentId=Jon_Snow_22, subjects=[Maths, Science]))
That’s all about parsing a JSON String in Kotlin.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)