Java GSON编码/解码复杂的类结构

时间:2016-11-06 11:47:31

标签: java android json gson

Class1.java文件:

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

Class2.java文件:

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

这些是我的类,我想用GSON将 Class1 对象解析为JSON字符串。 这很好用:

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

并产生类似的东西:

{
    "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}
                ]
        }
    ]
}

但我不知道如何稍后从JSON字符串创建(解码)此对象结构。 或者解码的唯一方法是手动循环遍历String,并创建所有对象?

2 个答案:

答案 0 :(得分:1)

我找不到代码的问题。这对我有用。

POJO课程

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解析代码

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'

答案 1 :(得分:0)

您可以使用gson.fromJson(jsonString,Class)

在你的情况下会是这样的:

Class1 obj=gson.fromJson(stringJson,Class1.class)