gpt4 book ai didi

java - 使用 Gson,如何解析具有复杂结构的 JSON 响应

转载 作者:行者123 更新时间:2023-12-01 09:59:22 24 4
gpt4 key购买 nike

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

流量统计列表:

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);

关于java - 使用 Gson,如何解析具有复杂结构的 JSON 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23950541/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com