I am trying to read a JSON file from this URL using ion, a library for Android, and Gson.
The JSON file in its current state:
{
"Excited":["https://github.com/vedantroy/image-test/raw/master/Happy/excited1.gif",
"https://github.com/vedantroy/image-test/raw/master/Happy/excited2.gif",
"https://github.com/vedantroy/image-test/raw/master/Happy/excited3.gif"],
"Sad":["https://github.com/vedantroy/image-test/raw/master/Sad/sad1.gif",
"https://github.com/vedantroy/image-test/raw/master/Sad/sad2.gif",
"https://github.com/vedantroy/image-test/raw/master/Sad/sad3.gif",
"https://github.com/vedantroy/image-test/raw/master/Sad/sad4.gif"]
}
Important note: While right now the JSON file has two arrays, "Excited" and "Sad", it may have more arrays in the future. However, the base structure of the file will always be a series of JSON arrays.
My objective is to convert this JSON object containing two (but could be more) arrays into a list of lists.
Here is my code so far, it parses the URL and returns a Gson JsonObject
called "result":
Ion.with(applicationContext)
.load("https://raw.githubusercontent.com/vedantroy/image-test/master/index.json")
.asJsonObject()
.setCallback { e, result ->
//Do something...
}
This code can also be written as:
Ion.with(applicationContext)
.load("https://raw.githubusercontent.com/vedantroy/image-test/master/index.json")
.asJsonObject()
.setCallback(object : FutureCallback<JsonObject> {
override fun onCompleted(e: Exception?, result: JsonObject?) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
})
Loop through the JsonObject
entrySet
and add each JsonArray
into the list, I use my Java code, you can convert it to Kotlin if need:
ArrayList<JsonArray> result = new ArrayList<>();
for (Map.Entry<String, JsonElement> entry : resultJson.entrySet()) {
if (entry.getValue().isJsonArray()) {
result.add(entry.getValue().getAsJsonArray());
}
}