3

如您所见,对象matchField操作是包含具有不同成员的对象的数组。请说出我的类结构应该是什么来解析这个 JSON 数据,以便我可以获得所有数据(请注意,n matchField操作中的对象可以有其他成员 - 不仅仅是这个响应中的那些)。还有其他方法可以使用 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)
4

1 回答 1

0

以下是您需要创建的 POJO 类,以将 JSON 数据解析为相应的 java 对象

流量统计列表:

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
}

流量统计:

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
}

比赛场:

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);
于 2015-01-08T07:50:55.830 回答