I need to parse very large JSON files that are downloaded from a server. These JSON files can contain completely different keys and values. Here are some examples...
{ "result": "PASS",
"items": [
{ "name": "John", "age": 33 },
{ "name": "Jane", "age": 23 } ]
}
{ "result": "PASS",
"items": [
{ "make": "Ford", "model": "Mustang", "colors": ["blue", "red", "silver"] },
{ "make": "Dodge", "model": "Charger", "colors": ["yellow", "black", "silver"] } ]
}
The items
array can potentially contain thousands of entries and the data within each item can contain up to 60 key/value pairs.
These are just two examples but I need to be able to parse 30-40 different types of JSON files and I can't always control what type of data is in the file. Because of this, I cannot create custom models to bind the data to objects in my app.
What I'm trying to do is create a JsonObject
for each item in the items
array and add it to a MutableList
that I can use in the app. I am currently using the Klaxon Streaming API to try and accomplish this but can seem to find a way to do it without binding to a custom object.
JsonReader(StringReader(testJson)).use { reader ->
reader.beginObject {
var result: String? = null
while (reader.hasNext()) {
val name = reader.nextName()
when (name) {
"result" -> result = reader.nextString()
"items" -> {
reader.beginArray {
while (reader.hasNext()) {
// ???
}
}
}
}
}
}
}