kotlin read json object code example
Example 1: kotlin json
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
dependencies {
implementation 'com.google.code.gson:gson:2.8.5'
}
data class TestModel(
val id: Int,
val description: String
)
var gson = Gson()
var jsonString = gson.toJson(TestModel(1,"Test"))
var jsonString = """{"id":1,"description":"Test"}""";
var testModel = gson.fromJson(jsonString, TestModel::class.java)
Example 2: kotlin jsonobject from string
val data = JSONObject("""{"name":"test name", "age":25}""")