簡體   English   中英

使用Gson,如何解析具有復雜結構的JSON響應

[英]With Gson, how to parse JSON response having complex structure

如您所見,對象matchFieldactions是包含具有不同成員的對象的數組。 請說明我的類結構應該解析這個JSON數據,以便我可以獲取所有數據(請注意,n matchFieldactions中的對象可以有其他成員 - 不僅僅是此響應中的對象)。 還有其他方式使用GSON(除了使用gson.fromJson )來完成這項工作嗎?

{
"node": {
    "id": "00:00:00:00:00:00:00:01",
    "type": "OF"
},
"flowStatistic": [
    {
        "flow": {
            "match": {
                "matchField": [
                    {
                        "type": "DL_TYPE",
                        "value": "2048"
                    },
                    {
                        "mask": "255.255.255.255",
                        "type": "NW_DST",
                        "value": "10.0.0.1"
                    }
                ]
            },
            "actions": [
                {
                    "type": "SET_DL_DST",
                    "address": "7a11761ae595"
                },
                {
                    "type": "OUTPUT",
                    "port": {
                        "node": {
                            "id": "00:00:00:00:00:00:00:01",
                            "type": "OF"
                        },
                        "id": "1",
                        "type": "OF"
                    }
                }
            ],
            "priority": 1,
            "idleTimeout": 0,
            "hardTimeout": 0,
            "id": 0
        },
        "tableId": 0,
        "durationSeconds": 62500,
        "durationNanoseconds": 513000000,
        "packetCount": 0,
        "byteCount": 0
    },
    {
        "flow": {
            "match": {
                "matchField": [
                    {
                        "type": "DL_TYPE",
                        "value": "2048"
                    },
                    {
                        "mask": "255.255.255.255",
                        "type": "NW_DST",
                        "value": "10.0.0.2"
                    }
                ]
            },
            "actions": [
                {
                    "type": "OUTPUT",
                    "port": {
                        "node": {
                            "id": "00:00:00:00:00:00:00:01",
                            "type": "OF"
                        },
                        "id": "2",
                        "type": "OF"
                    }
                }
            ],
            "priority": 1,
            "idleTimeout": 0,
            "hardTimeout": 0,
            "id": 0
        },
        "tableId": 0,
        "durationSeconds": 62500,
        "durationNanoseconds": 508000000,
        "packetCount": 0,
        "byteCount": 0
    },
    {
        "flow": {
            "match": {
                "matchField": [
                    {
                        "type": "DL_TYPE",
                        "value": "2048"
                    },
                    {
                        "type": "IN_PORT",
                        "value": "OF|2@OF|00:00:00:00:00:00:00:01"
                    }
                ]
            },
            "actions": [
                {
                    "type": "SET_NW_TOS",
                    "tos": 30
                }
            ],
            "priority": 500,
            "idleTimeout": 0,
            "hardTimeout": 0,
            "id": 0
        },
        "tableId": 0,
        "durationSeconds": 62252,
        "durationNanoseconds": 633000000,
        "packetCount": 0,
        "byteCount": 0
    }
]

}

以下是創建的POJO

public class FlowStatisticsList {

@Expose
@SerializedName("node")
private Node node;
@Expose
@SerializedName("flowStatistic")
private List<FlowStatistic> flowStatistic = new ArrayList<FlowStatistic>();

public Node getNode() {
    return node;
}

public void setNode(Node node) {
    this.node = node;
}

public List<FlowStatistic> getFlowStatistic() {
    return flowStatistic;
}

public void setFlowStatistic(List<FlowStatistic> flowStatistic) {
    this.flowStatistic = flowStatistic;
}

}

public class FlowStatistic {

@Expose
@SerializedName("flow")
private Flow flow;
@Expose
@SerializedName("tableId")
private long tableId;
@Expose
@SerializedName("durationSeconds")
private long durationSeconds;
@Expose
@SerializedName("durationNanoseconds")
private long durationNanoseconds;
@Expose
@SerializedName("packetCount")
private long packetCount;
@Expose
@SerializedName("byteCount")
private long byteCount;

public Flow getFlow() {
    return flow;
}

public void setFlow(Flow flow) {
    this.flow = flow;
}

public long getTableId() {
    return tableId;
}

public void setTableId(long tableId) {
    this.tableId = tableId;
}

public long getDurationSeconds() {
    return durationSeconds;
}

public void setDurationSeconds(long durationSeconds) {
    this.durationSeconds = durationSeconds;
}

public long getDurationNanoseconds() {
    return durationNanoseconds;
}

public void setDurationNanoseconds(long durationNanoseconds) {
    this.durationNanoseconds = durationNanoseconds;
}

public long getPacketCount() {
    return packetCount;
}

public void setPacketCount(long packetCount) {
    this.packetCount = packetCount;
}

public long getByteCount() {
    return byteCount;
}

public void setByteCount(long byteCount) {
    this.byteCount = byteCount;
}

}

public class Flow {

@Expose
@SerializedName("match")
private Match match;
@Expose
@SerializedName("actions")
private List<Action> actions = new ArrayList<Action>();
@Expose
@SerializedName("priority")
private long priority;
@Expose
@SerializedName("idleTimeout")
private long idleTimeout;
@Expose
@SerializedName("hardTimeout")
private long hardTimeout;
@Expose
@SerializedName("id")
private long id;

public Match getMatch() {
    return match;
}

public void setMatch(Match match) {
    this.match = match;
}

public List<Action> getActions() {
    return actions;
}

public void setActions(List<Action> actions) {
    this.actions = actions;
}

public long getPriority() {
    return priority;
}

public void setPriority(long priority) {
    this.priority = priority;
}

public long getIdleTimeout() {
    return idleTimeout;
}

public void setIdleTimeout(long idleTimeout) {
    this.idleTimeout = idleTimeout;
}

public long getHardTimeout() {
    return hardTimeout;
}

public void setHardTimeout(long hardTimeout) {
    this.hardTimeout = hardTimeout;
}

public long getId() {
    return id;
}

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

}

public class Match {

@Expose
@SerializedName("matchField")
private List<MatchField> matchField = new ArrayList<MatchField>();

public List<MatchField> getMatchField() {
    return matchField;
}

public void setMatchField(List<MatchField> matchField) {
    this.matchField = matchField;
}

}

我堅持為Action和MatchField創建POJO。

以下代碼段用於反序列化響應

gson.fromJson(jsonString, FlowStatisticsList.class)

以下是您需要創建的POJO類,以將JSON數據解析為相應的java對象

FlowStatisticsList:

public class FlowStatisticsList {
    private Node node;

    private List<FlowStatistic> flowStatistic = new ArrayList<FlowStatistic>();

    // getters, setters & toString methods
}

節點:

public class Node {
    private String id;
    private String type;

    // getters, setters & toString methods
}

FlowStatistic:

public class FlowStatistic {
    private Flow flow;

    private long tableId;
    private long durationSeconds;
    private long durationNanoseconds;
    private long packetCount;
    private long byteCount;

    // getters, setters & toString methods
}

流:

public class Flow {
    private Match match;

    private List<Action> actions = new ArrayList<Action>();

    private long priority;
    private long idleTimeout;
    private long hardTimeout;
    private long id;

    // getters, setters & toString methods
}

比賽:

public class Match {
    private List<MatchField> matchField = new ArrayList<MatchField>();

    // getters, setters & toString methods
}

MatchField:

public class MatchField {
    private String mask;
    private String type;
    private String value;

    // getters, setters & toString methods
}

行動:

public class Action {
    private String type;
    private String address;
    private int tos;

    private Port port;

    // getters, setters & toString methods
}

港口:

public class Port {
    private Node node;

    private String type;
    private String id;

    // getters, setters & toString methods
}

最后解析如下:

Gson gson = new GsonBuilder().create();
FlowStatisticsList object = gson.fromJson(jsonData, FlowStatisticsList.class);
System.out.println(object);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM