Роман Дервишов Роман Дервишов - 4 months ago 11
JSON Question

GSON to parse a JSON array

I have a JSON file like this:

[
{
"id": "1",
"name": "test1",
"childrens": [
{
"id": "14",
"name": "test2",
"childrens": [

]
}
]
}
]


The model class:

public class Model {

private int id;
private String name;
}


And my parse method:

public List< Model > parseJSONService(JSONArray jsonArray) {

Gson gson = new Gson();
Model[] model = gson.fromJson(jsonArray.toString(),
Model[].class);
return Arrays.asList(model);
}

Answer

why not parse the json string directly? this is what i did, achieved the same thing you looking for i guess:

public class GsonPlay {


    public static void main(String args[]) {

        String testString = "[{\"id\": \"1\",\"name\": \"test1\",\"childrens\": [{\"id\": \"14\",\"name\": \"test2\",\"childrens\": []}]}]";
        List<Model> modelList = parseJsonService(testString);
        System.out.println(modelList);
    }

    private static List<Model> parseJsonService(String testString) {
        Gson gson = new Gson();
        Model[] models = gson.fromJson(testString, Model[].class);
        return Arrays.asList(models);
    }
}
class Model {
    private int id;
    private String name;
    private List<Model> childrens;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Model> getChildrens() {
        return childrens;
    }

    public void setChildrens(List<Model> childrens) {
        this.childrens = childrens;
    }
}

You can also have a look at this for further ideas: