I have an Array of Json elements as a String, my problem is how to convert them into and Object Array using Gson.
I found some methods on this site but non of them seem to work for my String.
["2": {"id": 2, "name": "Cannonball", "sp": 5, "overall_average": 194, "buy_average": 193, "members": true, "sell_average": 193},
"6": {"id": 6, "name": "Cannon base", "sp": 187500, "overall_average": 188110, "buy_average": 184547, "members": true, "sell_average": 185735},
"12289": {"id": 12289, "name": "Mithril platelegs (t)", "sp": 2600, "overall_average": 0, "buy_average": 3000, "members": false, "sell_average": 3000},
"8": {"id": 8, "name": "Cannon stand", "sp": 187500, "overall_average": 198445, "buy_average": 189001, "members": true, "sell_average": 190889},
"10": {"id": 10, "name": "Cannon barrels", "sp": 187500, "overall_average": 194418, "buy_average": 185164, "members": true, "sell_average": 185935},
"12": {"id": 12, "name": "Cannon furnace", "sp": 187500, "overall_average": 188000, "buy_average": 186524, "members": true, "sell_average": 186637},
"4099": {"id": 4099, "name": "Mystic hat (dark)", "sp": 15000, "overall_average": 9758, "buy_average": 9229, "members": true, "sell_average": 9528}]
I need to convert the data to this java object.
public class OSBuddyItem {
private final int id;
private final String name;
private final int sellPrice;
private final int buyPrice;
private final int averagePrice;
private final int storePrice;
private final boolean members;
public OSBuddyItem(int id, String name, int sellPrice, int buyPrice, int averagePrice, int storePrice, boolean members){
this.id = id;
this.name = name;
this.sellPrice = sellPrice;
this.buyPrice = buyPrice;
this.averagePrice = averagePrice;
this.storePrice = storePrice;
this.members = members;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getSellPrice() {
return sellPrice;
}
public int getBuyPrice() {
return buyPrice;
}
public int getAveragePrice() {
return averagePrice;
}
public int getStorePrice() {
return storePrice;
}
public boolean isMembers() {
return members;
}
}
This is what I tried:
Gson gson = new Gson();
JsonParser parser = new JsonParser();
JsonElement tradeElement = parser.parse(data);
JsonArray itemElements = tradeElement.getAsJsonArray();
OSBuddyItem[] items = gson.fromJson(itemElements,OSBuddyItem[].class);
for(OSBuddyItem item : items){
System.out.println(item.getName());
}
Can someone please tell me how to convert the String using Gson?
Your JSON String is not valid.You can validate your JSON String from [http://jsoneditoronline.org/][1]
Suppose if JSON String is in form :-
[{"1": {"id": 2, "name": "Cannonball", "sp": 5, "overall_average": 194, "buy_average": 193, "members": true, "sell_average": 193}}]
Suppose Model Name is Example.Then Model of Json will be :-
package com.example;
import com.google.gson.annotations.SerializedName;
public class Example {
#SerializedName("id")
private Integer id;
#SerializedName("name")
private String name;
#SerializedName("sp")
private Integer sp;
#SerializedName("overall_average")
private Integer overallAverage;
#SerializedName("buy_average")
private Integer buyAverage;
#SerializedName("members")
private Boolean members;
#SerializedName("sell_average")
private Integer sellAverage;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getSp() {
return sp;
}
public void setSp(Integer sp) {
this.sp = sp;
}
public Integer getOverallAverage() {
return overallAverage;
}
public void setOverallAverage(Integer overallAverage) {
this.overallAverage = overallAverage;
}
public Integer getBuyAverage() {
return buyAverage;
}
public void setBuyAverage(Integer buyAverage) {
this.buyAverage = buyAverage;
}
public Boolean getMembers() {
return members;
}
public void setMembers(Boolean members) {
this.members = members;
}
public Integer getSellAverage() {
return sellAverage;
}
public void setSellAverage(Integer sellAverage) {
this.sellAverage = sellAverage;
}
}
And using GSON ,you can convert like below.
[Note :TypeToken class is used to load JSON String into a custom Object]
List<Example> myList = new Gson().fromJson(br, new TypeToken<List<JsonLog>>(){}.getType());
Related
De-Serializing Objects within a List
I currently have an 'Admin' Entity in which upon a GET Request of AllAdmin() will return me the following response. This was used in Postman.
GET Response for AllAdmin() [POSTMAN]
[
{
"adminId": 1,
"fullName": "Patrick ",
"email": "patrick#gmail.com",
"dob": "1669-12-12",
"mobileNumber": "96369636",
"password": "password123!",
"usages": [
{
"id": 3,
"datetimeUnlocked": "2021-06-07 10:12:23"
},
{
"id": 4,
"datetimeUnlocked": "2021-06-07 10:12:27"
}
],
"authorization": [
{
"id": 2,
"datetimeAccepted": "2021-06-07 10:12:14"
}
],
"adminAllow": []
},
{
"adminId": 2,
"fullName": "Worker ",
"email": "worker#gmail.com",
"dob": "2000-12-12",
"mobileNumber": "96399639",
"password": "password123!",
"usages": [],
"authorization": [],
"adminAllow": []
} ]
The current code is my Admin Model in my Android Application.
Admin.java Model Class
public class Admin {
#SerializedName("adminId")
private long adminID;
#SerializedName("fullName")
private String adminFullName;
#SerializedName("email")
private String adminEmail;
#SerializedName("dob")
private String adminDOB;
#SerializedName("mobileNumber")
private String adminMobileNumber;
// Constructor
public Admin(long adminID, String adminFullName, String adminEmail, String adminDOB, String adminMobileNumber) {
this.adminID = adminID;
this.adminFullName = adminFullName;
this.adminEmail = adminEmail;
this.adminDOB = adminDOB;
this.adminMobileNumber = adminMobileNumber;
}
// Getter
public long getAdminID() {
return adminID;
}
public String getAdminFullName() {
return adminFullName;
}
public String getAdminEmail() {
return adminEmail;
}
public String getAdminDOB() {
return adminDOB;
}
public String getAdminMobileNumber() {
return adminMobileNumber;
}
}
I would like to clarify on how I would de-serialize the usages and authorization properties so that I am able to access and manipulate the data entries for these?
I thank you in advance for your clarifications!
For usages, you should use a List<Usage> as the serializable. My assumption is you are using Retrofit. So the Retrofit Gson converter will take care of parsing the array of Usage. The same logic would apply for authorization. You can use the different key there for the datetimeAccepted case.
Check this sample for Usage
public class Usage {
#SerializedName("id")
#Expose
private Integer id;
#SerializedName("datetimeUnlocked")
#Expose
private String datetimeUnlocked;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getDatetimeUnlocked() {
return datetimeUnlocked;
}
public void setDatetimeUnlocked(String datetimeUnlocked) {
this.datetimeUnlocked = datetimeUnlocked;
}
}
Your updated Admin class must look like this
public class Admin {
#SerializedName("adminId")
private long adminID;
#SerializedName("fullName")
private String adminFullName;
#SerializedName("email")
private String adminEmail;
#SerializedName("dob")
private String adminDOB;
#SerializedName("mobileNumber")
private String adminMobileNumber;
#SerializedName("usages")
private List<Usage> usages;
// Constructor
public Admin(long adminID, String adminFullName, String adminEmail, String adminDOB, String adminMobileNumber, List<Usage> usages) {
this.adminID = adminID;
this.adminFullName = adminFullName;
this.adminEmail = adminEmail;
this.adminDOB = adminDOB;
this.adminMobileNumber = adminMobileNumber;
this.usages = usages;
}
// Getter
public long getAdminID() {
return adminID;
}
public String getAdminFullName() {
return adminFullName;
}
public String getAdminEmail() {
return adminEmail;
}
public String getAdminDOB() {
return adminDOB;
}
public String getAdminMobileNumber() {
return adminMobileNumber;
}
public List<Usage> getUsages() {
return usages;
}
}
I am using Retrofit android library and Gson converter to get JSON response from my server,
this is the response. After receiving the response my app is crashing in onCreate()API. Could anyone suggest me what is wrong with below code?
{
"content": [
{
"id": 1,
"title": "Xrp the standard",
"desc": "Hodl till you die",
"createdBy": {
"id": 1,
"username": "1",
"name": "Radovan"
},
"creationDateTime": {
"epochSecond": 1533679200,
"nano": 0
},
"photo": "to_the_moon.jpg",
"tags": "tagovi",
"likes": 12,
"dislikes": 2,
"comments": [
{
"id": 1,
"title": "Bravo nasi",
"desc": "Samo sloga Srbina spasava",
"createdBy": {
"id": 2,
"username": "",
"name": ""
},
"creationDateTime": {
"epochSecond": 1533679200,
"nano": 0
},
"likes": 3,
"dislikes": 4
}
]
}
],
"page": 0,
"size": 30,
"totalElements": 1,
"totalPages": 1,
"last": true
}
This is my Retro client,
public class RetroClient {
private static final String BASE_URL = "http://192.189.0.27:8080/";
private static Retrofit getRetrofitInstance() {
return new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
public static RestAPI getRestAPI() {
return getRetrofitInstance().create(RestAPI.class);
}
}
This is my rest api call
#GET("api/posts")
Call<Example> getPosts();
My Example class
public class Example {
#SerializedName("content")
#Expose
private List<Content> content;
#SerializedName("page")
#Expose
private Integer page;
#SerializedName("size")
#Expose
private Integer size;
#SerializedName("totalElements")
#Expose
private Integer totalElements;
#SerializedName("totalPages")
#Expose
private Integer totalPages;
#SerializedName("last")
#Expose
private Boolean last;
public Example() {
}
public Example(List<Content> content, Integer page, Integer size, Integer totalElements, Integer totalPages, Boolean last) {
super();
this.content = content;
this.page = page;
this.size = size;
this.totalElements = totalElements;
this.totalPages = totalPages;
this.last = last;
}
//Getters and setters
My Content class (nested array of objects which I need access to, "content" in response)
public class Content {
#SerializedName("id")
#Expose
private Integer id;
#SerializedName("title")
#Expose
private String title;
#SerializedName("desc")
#Expose
private String desc;
#SerializedName("createdBy")
#Expose
private CreatedBy createdBy;
#SerializedName("creationDateTime")
#Expose
private CreationDateTime creationDateTime;
#SerializedName("photo")
#Expose
private String photo;
#SerializedName("tags")
#Expose
private String tags;
#SerializedName("likes")
#Expose
private Integer likes;
#SerializedName("dislikes")
#Expose
private Integer dislikes;
#SerializedName("comments")
#Expose
private List<CommentResponse> comments;
public Content() {
}
public Content(Integer id, String title, String desc, CreatedBy createdBy, CreationDateTime creationDateTime, String photo, String tags, Integer likes, Integer dislikes, List<CommentResponse> comments) {
super();
this.id = id;
this.title = title;
this.desc = desc;
this.createdBy = createdBy;
this.creationDateTime = creationDateTime;
this.photo = photo;
this.tags = tags;
this.likes = likes;
this.dislikes = dislikes;
this.comments = comments;
}
//Getters and setters
and finally, my callback method in onCreate
RestAPI rest_api = RetroClient.getRestAPI();
Call<Example> call = rest_api.getPosts();
call.enqueue(new Callback<Example>() {
#Override
public void onResponse(Call<Example> call, Response<Example> response) {
if( response.isSuccessful()) {
//here, I need to get "content" so I could fill up list in code below,
//tried almost everything, but activity crashes when created
postsList = response.body().getContent();
for(int i = 0; i < postsList.size(); i++) {
Content content = postsList.get(i);
pAdapter.addPost(content);
}
}
else {
int sc = response.code();
switch (sc) {
}
}
}
#Override
public void onFailure(Call<Example> call, Throwable t) {
Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
Add getters and setters for list "content" in Example class and do this "response.body().getContent()" to get the list.
postsList = response.body().getContent();
Your Pojos should implements Parcelable interface
public class Example implements Parcelable {
//your members
}
I am new to gson and getting this error.
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2527 path $.data.batting[0].scores[1].dismissal-by
it is because of the different json reply given by the API.
this is the json reply:
"batting": [
{
"scores": [
{
"dismissal-by": {
"name": "CE Rudd",
"pid": "646213"
},
"dismissal": "stumped",
"SR": 126,
"6s": 0,
"4s": 5,
"B": 34,
"R": 43,
"dismissal-info": "st Rudd b Kerr",
"batsman": "NE Bolton",
"pid": "267611"
},
{
"dismissal-by": [
{
"name": "M du Preez",
"pid": "54646"
}
],
"dismissal": "runout",
"SR": 112,
"6s": 0,
"4s": 4,
"B": 25,
"R": 28,
"dismissal-info": "run out (du Preez)",
"batsman": "GEB Boyce",
"pid": "874261"
},
{
"dismissal-by": {
"name": "LK Bell",
"pid": "878025"
},
"dismissal": "catch",
"SR": 100,
"6s": 0,
"4s": 2,
"B": 27,
"R": 27,
"dismissal-info": "c Bell b Scholfield",
"batsman": "AE Satterthwaite",
"pid": "233007"
},
{
"dismissal": "not out",
"SR": 220,
"6s": 2,
"4s": 5,
"B": 20,
"R": 44,
"dismissal-info": "not out",
"batsman": "H Kaur",
"pid": "372317"
},
{
"dismissal": "not out",
"SR": 100,
"6s": 0,
"4s": 1,
"B": 14,
"R": 14,
"dismissal-info": "not out",
"batsman": "E Threlkeld ",
"pid": "878035"
},
{
"SR": "",
"6s": "",
"4s": "",
"B": "",
"R": "",
"dismissal-info": "",
"detail": "6 (b 1, w 5)",
"batsman": "Extras",
"pid": 0
}
],
"title": "Lancashire Thunder Innings"
},
getting the error at the 2nd dismissal-by object.
the 1st dismissal-by starts with an object and the second dismissal-by object by an array.
this is the java class for the scores array
public class Score__ implements Serializable {
#SerializedName("dismissal-by")
#Expose
private DismissalBy dismissalBy;
#SerializedName("dismissal")
#Expose
private String dismissal;
#SerializedName("SR")
#Expose
private String sR;
#SerializedName("6s")
#Expose
private String _6s;
#SerializedName("4s")
#Expose
private String _4s;
#SerializedName("B")
#Expose
private String b;
#SerializedName("R")
#Expose
private String r;
#SerializedName("dismissal-info")
#Expose
private String dismissalInfo;
#SerializedName("batsman")
#Expose
private String batsman;
#SerializedName("pid")
#Expose
private Integer pid;
#SerializedName("detail")
#Expose
private String detail;
public DismissalBy getDismissalBy() {
return dismissalBy;
}
public void setDismissalBy(DismissalBy dismissalBy) {
this.dismissalBy = dismissalBy;
}
public String getDismissal() {
return dismissal;
}
public void setDismissal(String dismissal) {
this.dismissal = dismissal;
}
public String getSR() {
return sR;
}
public void setSR(String sR) {
this.sR = sR;
}
public String get6s() {
return _6s;
}
public void set6s(String _6s) {
this._6s = _6s;
}
public String get4s() {
return _4s;
}
public void set4s(String _4s) {
this._4s = _4s;
}
public String getB() {
return b;
}
public void setB(String b) {
this.b = b;
}
public String getR() {
return r;
}
public void setR(String r) {
this.r = r;
}
public String getDismissalInfo() {
return dismissalInfo;
}
public void setDismissalInfo(String dismissalInfo) {
this.dismissalInfo = dismissalInfo;
}
public String getBatsman() {
return batsman;
}
public void setBatsman(String batsman) {
this.batsman = batsman;
}
public Integer getPid() {
return pid;
}
public void setPid(Integer pid) {
this.pid = pid;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
}
the dismissal-by java class
public class DismissalBy implements Serializable {
#SerializedName("name")
#Expose
private String name;
#SerializedName("pid")
#Expose
private String pid;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPid() {
return pid;
}
public void setPid(String pid) {
this.pid = pid;
}
}
how do i fix this problem ?
any help will be appreciated
in your JSON object, there is a
"dismissal-by": {
"name": "LK Bell",
"pid": "878025"
},
you cannot use it like this since you declare it as an array it should stay at the same type there for you need to change it to something like
"dismissal-by"":[
{
"name": "LK Bell",
"pid": "878025"
}
]
,
to fix this issue you need to parse all JSON manually like
Json json = new Json(string);
try{
json.getJsonArray("dismissal-by");
}catch(IllegalStateException e)
{
json.getObject("dismissal-by");
}
that JSON is invalid... therefore the com.google.gson.JsonSyntaxException.
it needs to start with a { and after "title": "Lancashire Thunder Innings", there is a }] missing.
there you can check for yourself: https://jsonlint.com
The dismissal-by you are getting is Some time in array format (start with [ and end with ]) while some time it is Json object {} it will be better to resolve it from API developer. Or you can declare it as String and do parsing later where required.
public class Score__ implements Serializable {
#SerializedName("dismissal-by")
#Expose
private String dismissalBy;}
I have two entities.
The problem i am currently facing is that i get repeated json values even though i only have one row of data inserted on my database.
My Questions.java entity looks like this.
Questions.java
#Entity
#Table
public class Questions {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "id")
private int id;
#Column(name = "question")
private String question;
#Column(name = "type")
private String type;
#Column(name = "description")
private String description;
#Column(name = "param")
private String param;
#Column(name = "maxlength")
private int maxlength;
#Column(name = "dependency")
private String dependency;
#OneToMany(mappedBy = "questions",targetEntity = Answers.class)
private List<Answers> answers = new ArrayList<>();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getParam() {
return param;
}
public void setParam(String param) {
this.param = param;
}
public int getMaxlength() {
return maxlength;
}
public void setMaxlength(int maxlength) {
this.maxlength = maxlength;
}
public String getDependency() {
return dependency;
}
public List<Answers> getAnswers() {
return answers;
}
public void setAnswers(List<Answers> answers) {
this.answers = answers;
}
public void setDependency(String dependency) {
this.dependency = dependency;
}
}
and here is another entity answers.java
Answers.java
#Entity
public class Answers {
#Id
#Column(name = "id")
private int id;
#Column(name = "ans_label")
private String ans_label;
#Column(name = "ans_value")
private int ans_value;
#Column(name = "ans_weightage")
private int ans_weightage;
#Column(name = "is_default")
private int is_default;
#ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
#JoinColumn(name = "question_id")
private Questions questions;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAns_label() {
return ans_label;
}
public void setAns_label(String ans_label) {
this.ans_label = ans_label;
}
public int getAns_value() {
return ans_value;
}
public void setAns_value(int ans_value) {
this.ans_value = ans_value;
}
public int getAns_weightage() {
return ans_weightage;
}
public void setAns_weightage(int ans_weightage) {
this.ans_weightage = ans_weightage;
}
public int getIs_default() {
return is_default;
}
public void setIs_default(int is_default) {
this.is_default = is_default;
}
public Questions getQuestions() {
return questions;
}
public void setQuestions(Questions questions) {
this.questions = questions;
}
}
My controller which saves data (testing ) as well as returns json.
SaveApiContoller.java
#Controller
public class SaveApiController {
#Autowired
SaveApiServices saveApiServices;
#Autowired
SaveQuestions saveQuestions;
#RequestMapping(value = "/saveData", method = RequestMethod.POST)
public String saveData(#RequestBody Answers answerss, Questions questionss){
Answers answers = new Answers();
answers.setAns_label("मुली");
answers.setAns_value(1);
answers.setAns_weightage(0);
answers.setIs_default(0);
Questions questions = new Questions();
questions.setQuestion("१. व्यक्तिको पुरा नाम थर ?");
questions.setType("input_edittext");
questions.setDescription("");
questions.setParam("smalltext");
questions.setMaxlength(20);
questions.setDependency("");
answers.setQuestions(questions);
saveApiServices.saveAnswers(answers);
return null;
}
#RequestMapping("/getData")
public #ResponseBody List<Questions> getData(){
List<Questions> questionss=saveApiServices.getQuestions();
return questionss;
}
}
I need to return json like this.
[
{
"section": "खण्ड ग",
"prefix": "c",
"description": "व्यक्तिगत विवरण",
"is_multiple": "1",
"check_weight": "1",
"questions": [
{
"id": "31",
"question": "३. लिङ्ग ?",
"type": "input_spinner",
"description": null,
"param": "",
"maxlength": "0",
"dependency": null,
"answers": [
{
"ans_label": "पुरुष",
"ans_value": "1",
"ans_weightage": "0",
"is_default": "0"
},
{
"ans_label": "महिला",
"ans_value": "2",
"ans_weightage": "0",
"is_default": "0"
},
{
"ans_label": "अन्य",
"ans_value": "3",
"ans_weightage": "0",
"is_default": "0"
}
]
},
{
"id": "33",
"question": "५. परिवारमूलीसँगको नाता",
"type": "input_spinner",
"description": null,
"param": "",
"maxlength": "1",
"dependency": null,
"answers": [
{
"ans_label": "नातिनी",
"ans_value": "15",
"ans_weightage": "0",
"is_default": "0"
},
{
"ans_label": "धर्म पुत्र वा सौतेनी छोरा",
"ans_value": "16",
"ans_weightage": "0",
"is_default": "0"
}
]
}
]
Unfortunately Currently i get a very long list of json as :
[{"id":1,"question":"१. व्यक्तिको पुरा नाम थर?", "type":"input_edittext",
"description":"","param":"smalltext","maxlength":20,"dependency":"",
"answers":
[{"id":0,"ans_label":"मुली","ans_value":1,"ans_weightage":0,"is_default":0,
"questions":{"id":1,"question":"१. व्यक्तिको पुरा नाम थर?",
"type":"input_edittext","description":"","param":"smalltext","maxlength":20
,"dependency":"","answers":[{"id":0,"ans_label":"मुली","ans_value":1,
"ans_weightage":0,"is_default":0,"questions":{"id":1,
"question":"१. व्यक्तिको पुरा नाम थर ?","type":"input_edittext",
"description":"","param":"smalltext","maxlength":20,"dependency":"",
"answers":[{"id":0,"ans_label":"मुली",
"ans_value":1,"ans_weightage":0,"is_default":0,"questions":{"id":1,
"question":"१. व्यक्तिको पुरा नाम थर ?","type":"input_edittext",
"description":"","param":"smalltext","maxlength":20,"dependency":"",
-----------------and more like this repeated----------------------
i am wondering why these same values are repeating even though my database table contains only one row.
i am wondering if cascade= CascadeType.All is being problem guy but if i remove casecade type from answers.java i won't be able to save data from controllers.
You do not use the params of the method saveData(#RequestBody ...), but new an answer and question object,which is just a record of the Question Object.
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)
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);