3

As you can see the objects matchField and actions are arrays holding objects having different members. Please say what should be my class structure to get this JSON data parsed so that I can get all the data (Please note that the objects in n matchField and actions can have other members- not only the ones in this response). Also is there any other way with GSON(other than using gson.fromJson) to get this done?

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

}

Following are the POJOs created

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

}

I'm stuck at creating POJOs for Action and MatchField.

The following snippet is used to deserialize the response

gson.fromJson(jsonString, FlowStatisticsList.class)
Po DW
  • 103
  • 1
  • 9
  • any code yet? [whathaveyoutried](http://whathaveyoutried.com) – Imperative May 30 '14 at 09:08
  • Yup.I created some POJOs to deal with this. I'll update this in question soon – Po DW May 30 '14 at 11:00
  • I still think it's simpler and easier to understand if you parse to a Map and write a constructor for your object that accepts the Map. Yes, you do actually have to write some assignment statements, but you can put everything right where you want it, and handling inner objects is as easy as calling their constructors with the appropriate inner Map. – Hot Licks May 30 '14 at 11:32
  • Can you please provide sample? – Po DW May 30 '14 at 13:09
  • Would you accept the answer which use [Jackson](https://github.com/FasterXML/jackson) instead of GSON? – Alexey Gavrilov May 31 '14 at 07:20
  • I'm planning to use this for Android. That's why I chose GSON – Po DW Jun 02 '14 at 04:55

1 Answers1

0

The following are the POJO classes that you need to create to parse the JSON data into corresponding java object

FlowStatisticsList:

public class FlowStatisticsList {
    private Node node;

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

    // getters, setters & toString methods
}

Node:

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
}

Flow:

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
}

Match:

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
}

Action:

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

    private Port port;

    // getters, setters & toString methods
}

Port:

public class Port {
    private Node node;

    private String type;
    private String id;

    // getters, setters & toString methods
}

and finally the parsing is as follows:

Gson gson = new GsonBuilder().create();
FlowStatisticsList object = gson.fromJson(jsonData, FlowStatisticsList.class);
System.out.println(object);
Prasad Khode
  • 6,602
  • 11
  • 44
  • 59