-1

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.
                    }

                })
Foobar
  • 7,458
  • 16
  • 81
  • 161
  • "My objective is to convert this JSON array into a list of lists" -- this is not a JSON array. It is a JSON object, holding arrays. Beyond that... what is your question? – CommonsWare May 18 '18 at 16:14
  • Is the use of gson mandatory for your solution? – Barns May 18 '18 at 16:17
  • @CommonsWare I'm sorry, I should have been more clear. I am trying to convert the JSON object that is holding the arrays into a list of lists. – Foobar May 18 '18 at 16:23
  • @Barns I think the use of Gson is mandatory because the Ion library is only compatible with Gson. – Foobar May 18 '18 at 16:23

2 Answers2

3

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());
    }
}
Tam Huynh
  • 2,026
  • 1
  • 16
  • 20
0

If you can refact this JSON, I would use something like this, for me this is easier to serialize/deserialize and looks more semantic (and is a JSON Array, the JSON you send is just a JSON Object)

[{
    "mood": "Excited",
    "gifs": ["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"
    ]
},

{
    "mood": "Sad",
    "gifs": ["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"
    ]
}
]

Using this JSON, you can deserialize as this in Java, so you will have to adapt to Kotlin using Ion library

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;

import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;

public class Deserializer {

    public static void main(String[] args) {
       JSONArray json = new JSONArray(
            "[{\"mood\": \"Excited\",\"gifs\": [\"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\"]},{\"mood\": \"Sad\",\"gifs\": [\"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\"]}]");
       Gson gson = new Gson();
       List<YourObject> test = gson.fromJson(json.toString(), new TypeToken<ArrayList<YourObject>>() {}.getType());
       test.forEach(System.out::println);
    }

    public static class YourObject {

        private String mood;
        private List<String> gifs;

        @Override
        public String toString() {
            return this.mood.toString() + " " + this.gifs.toString();
       }
    }
}
ghn1712
  • 105
  • 1
  • 3
  • 15