Gson Array of Array Deserialization

android, arraylist, gson, json, json-deserialization

Solution

gson.fromJson(json, Category[].class)

worked good for me. If you want an ArrayList instead of an Array:

new ArrayList<Category>(Arrays.asList(gson.fromJson(json, Category[].class)));

Problem

I've the following JSON structure: ``` [ { "id": 1, "subcategories": [ { "id": 2, "subcategories": [ ] }, { "id": 3, "subcategories": [ ] } ] }, { "id": 4, "subcategories": [ { "id": 5, subcategories: [ ] } ] } ] ``` The model class for a Category (some fields like title are omitted for simplicity), for reflecting JSON structure: ``` public class Category { int id = 0; ArrayList<Category> subCategories = null; } ``` I'm trying to parse that with Gson library (2.2.4), having hard times deserializing inner array to arraylist: ``` Gson gson = new Gson(); Type collectionType = new TypeToken<ArrayList<Category>>(){}.getType(); ArrayList categories = gson.fromJson(json, collectionType); ``` Category.subCategories is always null.

Original source

Related problems