thatsmyname thatsmyname - 27 days ago 11
JSON Question

Java GSON encoding/decoding complex Class structure

Class1.java file:

public class Class1{
private ArrayList<Class2> class2List;
...
}


Class2.java file:

public class Class2{
private Point point = new Point();
private ArrayList<Point> pointList;
...
}


These are my classes, and I want to parse a Class1 object to JSON string with GSON.
This works fine:

Gson gson = new Gson();
String json = gson.toJson(class1_object_name);


And generates something like that:

{
"class2List":[
{
"point":{"x":131,"y":304},
"pointList":
[
{"x":134,"y":319},
{"x":135,"y":333},
{"x":133,"y":348},
{"x":129,"y":349}
]
},

{
"point":{"x":311,"y":277},
"pointList":
[
{"x":312,"y":279},
{"x":315,"y":286},
{"x":318,"y":302},
{"x":321,"y":328},
{"x":321,"y":353}
]
}
]
}


But I don't know how to create (decode) this object-structure later from the JSON string.
Or the only way to decode is to manually loop through the String, and create all of the objects?

Answer

I couldn't find the problem with your code. This worked for me.

POJO classes

PointData.java

public class PointData {

    private List<Class2List> class2List = new ArrayList<Class2List>();

    public List<Class2List> getClass2List() {
        return class2List;
    }

    public void setClass2List(List<Class2List> class2List) {
        this.class2List = class2List;
    }

}

Class2List.java

public class Class2List {

    private Point point;
    private List<PointList> pointList = new ArrayList<PointList>();

    public Point getPoint() {
        return point;
    }

    public void setPoint(Point point) {
        this.point = point;
    }

    public List<PointList> getPointList() {
        return pointList;
    }

    public void setPointList(List<PointList> pointList) {
        this.pointList = pointList;
    }

}

Point.java

public class Point {

    private Integer x;
    private Integer y;

    public Integer getX() {
        return x;
    }

    public void setX(Integer x) {
        this.x = x;
    }

    public Integer getY() {
        return y;
    }

    public void setY(Integer y) {
        this.y = y;
    }

}

PointList.java

public class PointList {

    private Integer x;
    private Integer y;

    public Integer getX() {
        return x;
    }

    public void setX(Integer x) {
        this.x = x;
    }

    public Integer getY() {
        return y;
    }

    public void setY(Integer y) {
        this.y = y;
    }

}

JSON Parsing code

String data = "{\n" +
        "    \"class2List\":[\n" +
        "        {\n" +
        "            \"point\":{\"x\":131,\"y\":304},\n" +
        "            \"pointList\":\n" +
        "                [\n" +
        "                    {\"x\":134,\"y\":319},\n" +
        "                    {\"x\":135,\"y\":333},\n" +
        "                    {\"x\":133,\"y\":348},\n" +
        "                    {\"x\":129,\"y\":349}\n" +
        "                ]\n" +
        "        },\n" +
        "\n" +
        "        {\n" +
        "            \"point\":{\"x\":311,\"y\":277},\n" +
        "            \"pointList\":\n" +
        "                [\n" +
        "                    {\"x\":312,\"y\":279},\n" +
        "                    {\"x\":315,\"y\":286},\n" +
        "                    {\"x\":318,\"y\":302},\n" +
        "                    {\"x\":321,\"y\":328},\n" +
        "                    {\"x\":321,\"y\":353}\n" +
        "                ]\n" +
        "        }\n" +
        "    ]\n" +
        "}";

PointData parsedData = new Gson().fromJson(data, PointData.class);
Log.e("JSON", parsedData.getClass2List().get(0).getPoint().getX().toString());

build.gradle

compile 'com.google.code.gson:gson:2.8.0'
Comments