Since you are running your code with Junit, it running on the SDK on your computer. This SDK doesn't provide everything, some are just some skeleton class providing signature of method and documentation but not the code. So you can't execute directly.
Try to add the library in testing
testImplementation 'org.json:json:your_version'
See the version here :
http://mvnrepository.com/artifact/org.json/json
This is based on the answer on this post: JSONObject returns a non null value of "null" after instantiating with string
Answer from Evgeny Ezhov on Stack OverflowSince you are running your code with Junit, it running on the SDK on your computer. This SDK doesn't provide everything, some are just some skeleton class providing signature of method and documentation but not the code. So you can't execute directly.
Try to add the library in testing
testImplementation 'org.json:json:your_version'
See the version here :
http://mvnrepository.com/artifact/org.json/json
This is based on the answer on this post: JSONObject returns a non null value of "null" after instantiating with string
- There is no need to
substringyour JSON string. Put it insideJSONObjectand it will work fine. - If you need to get neasted objects use
getJSONObjectorgetJSONArraymethods - Show your
contentstring in code (or how do you load it)
Your edited code
val content = "the string above"
var obj = JSONObject(content)
java - How to parse JSON in Kotlin? - Stack Overflow
How to deserialize Json to java enum using Jackson ?
Help: Gson can not convert an kotlin object to json String?
Turning JSON array into Map
How to create a JSON object from a string in Kotlin?
How to convert string into JSON object in Android?
Videos
Perhaps I'm misunderstanding the question but it sounds like you are already using org.json which begs the question about why
val answer = JSONObject("""{"name":"test name", "age":25}""")
wouldn't be the best way to do it? What was wrong with the built in functionality of JSONObject?
val rootObject= JSONObject()
rootObject.put("name","test name")
rootObject.put("age","25")
There is no question that the future of parsing in Kotlin will be with kotlinx.serialization. It is part of Kotlin libraries. Version kotlinx.serialization 1.0 is finally released
https://github.com/Kotlin/kotlinx.serialization
Copyimport kotlinx.serialization.*
import kotlinx.serialization.json.Json
@Serializable
data class MyModel(val a: Int, @Optional val b: String = "42")
fun main(args: Array<String>) {
// serializing objects
val jsonData = Json.encodeToString(MyModel.serializer(), MyModel(42))
println(jsonData) // {"a": 42, "b": "42"}
// serializing lists
val jsonList = Json.encodeToString(MyModel.serializer().list, listOf(MyModel(42)))
println(jsonList) // [{"a": 42, "b": "42"}]
// parsing data back
val obj = Json.decodeFromString(MyModel.serializer(), """{"a":42}""")
println(obj) // MyModel(a=42, b="42")
}
You can use this library https://github.com/cbeust/klaxon
Klaxon is a lightweight library to parse JSON in Kotlin.