如您所见,对象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)