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);
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 trying to convert a complex object into related Java beans. However, some sub class can be not generated correctly.
I am just using simulate MS Adaptive card to create a set of Java beans classes. When I call Gson package or Alibaba fastJson package to parse my json data. it always shows the super class type.
This is just an experiment to test Gson & fastJson whether it can convert complex objects. which is running on the Android studio.
My demo json is like following:
{
"type": "AdaptiveCard",
"version": "1.0",
"id": "workloadQCactivity 20",
"speak": "activity 20 <break time=\"300ms\"/> at<break time=\"300ms\"/> <break time=\"300ms\"/>building<break time=\"300ms\"/>A<break time=\"300ms\"/>floor<break time=\"300ms\"/>1<break time=\"300ms\"/>room<break time=\"300ms\"/>1<break time=\"300ms\"/> ",
"body": [{
"type": "Container",
"items": [{
"type": "ColumnSet",
"columns": [{
"type": "Column",
"width": "Stretch",
"items": [{
"type": "TextBlock",
"size": "large",
"weight": "bolder",
"text": "activity 20 at building A floor 1 room 1",
"wrap": true
}]
}]
}]
}],
"actions": [{
"type": "Action.ShowCard",
"card": {
"type": "AdaptiveCard",
"version": "1.0",
"body": [{
"type": "TextBlock",
"size": "medium",
"weight": "bolder",
"isSubtle": true,
"text": "have thing to check list1",
"wrap": true
}, {
"type": "TextBlock",
"weight": "bolder",
"isSubtle": true,
"text": "this is section 1",
"wrap": true
}, {
"type": "TextBlock",
"isSubtle": true,
"text": "q1 of s1",
"wrap": true
}, {
"type": "TextBlock",
"isSubtle": true,
"text": "q2 of s2",
"wrap": true
}, {
"type": "TextBlock",
"weight": "bolder",
"isSubtle": true,
"text": "this is section 2",
"wrap": true
}, {
"type": "TextBlock",
"isSubtle": true,
"text": "q1 of s22",
"wrap": true
}, {
"type": "TextBlock",
"size": "medium",
"weight": "bolder",
"isSubtle": true,
"text": "have checklist 2",
"wrap": true
}, {
"type": "TextBlock",
"weight": "bolder",
"isSubtle": true,
"text": "section of the second checklist",
"wrap": true
}, {
"type": "TextBlock",
"isSubtle": true,
"text": "qqqqqq",
"wrap": true
}]
},
"title": "Show Checklist"
}]
}
Therefore, I just follow MS adaptive card to create following java beans.
First class: AdaptiveTypedElement
public class AdaptiveTypedElement {
#JsonProperty("additionalPorperties")
public Map<String, Object> additionalPorperties = new HashMap<String, Object>();
//#JsonProperty("type")
//public String type;
#JsonProperty("id")
public String id;
}
Second class: AdaptiveTypedElement
public class AdaptiveElement extends AdaptiveTypedElement {
#JsonProperty("spacing")
public AdaptiveSpacing spacing ;
#JsonProperty("separator")
public boolean separator = false;
#JsonProperty("speak")
public String speak;
#JsonProperty("separation")
// public AdaptiveSeparationStyle separation;
public String separation;
}
Third class AdaptiveContainer:
public class AdaptiveContainer extends AdaptiveElement {
#JsonProperty("typeName")
public String typeName = "Container";
#JsonProperty("type")
public String type = "Container";
#JsonProperty("items")
public List<AdaptiveElement> items = new ArrayList<AdaptiveElement>();
#JsonProperty("selectAction")
public AdaptiveAction selectAction = null;
#JsonProperty("style")
public AdaptiveContainerStyle style = AdaptiveContainerStyle.Default;
}
public class AdaptiveColumnSet extends AdaptiveElement {
#JsonProperty("typeName")
public final String typeName = "ColumnSet";
#JsonProperty("type")
public final String type = "ColumnSet";
#JsonProperty("columns")
public List<AdaptiveColumn> columns = new ArrayList<AdaptiveColumn>();
#JsonProperty("selectionAction")
public AdaptiveAction selectionAction = null;
}
public class AdaptiveColumn extends AdaptiveContainer{
#JsonProperty("typeName")
public final String typeName = "Column";
#JsonProperty("type")
public final String type = "Column";
#JsonProperty("size")
public String size;
#JsonProperty("with")
public String with;
}
public class AdaptiveAction {
#JsonProperty("title")
public String title;
#JsonProperty("speak")
public String speak;
}
public class AdaptiveShowCardAction extends AdaptiveAction {
#JsonProperty("typeName")
public final String typeName = "Action.ShowCard";
#JsonProperty("type")
public final String Type = "Action.ShowCard";
#JsonProperty("card")
public AdaptiveCard card;
}
public class AdaptiveTextBlock extends AdaptiveElement{
#JsonProperty("typeName")
public String typeName = "TextBlock";
#JsonProperty("type")
public String type = "TextBlock";
#JsonProperty("text")
public String text = "";
#JsonProperty("size")
public AdaptiveTextSize size;
#JsonProperty("weight")
public AdaptiveTextWeight weight;
#JsonProperty("color")
public AdaptiveTextColor color;
#JsonProperty("horizontalAlignment")
public AdaptiveHorizontalAlignment horizontalAlignment = AdaptiveHorizontalAlignment.Left;
#JsonProperty("wrap")
public boolean wrap = false;
#JsonProperty("isSubtle")
public boolean isSubtle = false;
#JsonProperty("maxLines")
public int maxLines = 0;
#JsonProperty("maxWidth")
public int maxWidth = 0;
}
public class AdaptiveCard extends AdaptiveTypedElement {
#JsonProperty("contentType")
public final String contentType = "application/vnd.microsoft.card.adaptive";
#JsonProperty("typeName")
public final String typeName = "AdaptiveCard";
#JsonProperty("type")
public String type = "AdaptiveCard";
#JsonProperty("body")
public List<AdaptiveElement> body = new ArrayList<AdaptiveElement>();
#JsonProperty("actions")
public List<AdaptiveAction> actions = new ArrayList<AdaptiveAction>();
#JsonProperty("speak")
public String speak = null;
#JsonProperty("title")
public String title;
#JsonProperty("version")
//public AdaptiveSchemaVersion version = null;
public String version = null;
#JsonProperty("fallbackText")
public String fallbackText = null;
#JsonProperty("lang")
public String lang = null;
}
In the end, I finally got AdaptiveCard Object. See my code:
return JSON.parseObject(attachJson, AdaptiveCard.class). Note that I add "implementation 'com.alibaba:fastjson:1.2.54'" in my Android Studio
When I check this object, I found the Body data member should be class "AdaptiveContainer " not "AdaptiveElement". I was wondering why it did not following subclass mechanism of OOP & OOD. I am expecting "AdaptiveContainer", but actually output is "AdaptiveElement".
enter image description here
rib-pet, your question has a far to big example. Anyway I tried to create a java class mapping for your sample using GSON
Action.java
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Action {
#SerializedName("type")
#Expose
private String type;
#SerializedName("card")
#Expose
private Card card;
#SerializedName("title")
#Expose
private String title;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Card getCard() {
return card;
}
public void setCard(Card card) {
this.card = card;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
Body.java
package com.example;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Body {
#SerializedName("type")
#Expose
private String type;
#SerializedName("items")
#Expose
private List<Item> items = null;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public List<Item> getItems() {
return items;
}
public void setItems(List<Item> items) {
this.items = items;
}
}
Body_.java (dependend on your wished model you should integrate this class better in some existing
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Body_ {
#SerializedName("type")
#Expose
private String type;
#SerializedName("size")
#Expose
private String size;
#SerializedName("weight")
#Expose
private String weight;
#SerializedName("isSubtle")
#Expose
private Boolean isSubtle;
#SerializedName("text")
#Expose
private String text;
#SerializedName("wrap")
#Expose
private Boolean wrap;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
public String getWeight() {
return weight;
}
public void setWeight(String weight) {
this.weight = weight;
}
public Boolean getIsSubtle() {
return isSubtle;
}
public void setIsSubtle(Boolean isSubtle) {
this.isSubtle = isSubtle;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Boolean getWrap() {
return wrap;
}
public void setWrap(Boolean wrap) {
this.wrap = wrap;
}
}
Card.java
package com.example;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Card {
#SerializedName("type")
#Expose
private String type;
#SerializedName("version")
#Expose
private String version;
#SerializedName("body")
#Expose
private List<Body_> body = null;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public List<Body_> getBody() {
return body;
}
public void setBody(List<Body_> body) {
this.body = body;
}
}
Column.java
package com.example;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Column {
#SerializedName("type")
#Expose
private String type;
#SerializedName("width")
#Expose
private String width;
#SerializedName("items")
#Expose
private List<Item_> items = null;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getWidth() {
return width;
}
public void setWidth(String width) {
this.width = width;
}
public List<Item_> getItems() {
return items;
}
public void setItems(List<Item_> items) {
this.items = items;
}
}
Example.java
package com.example;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Example {
#SerializedName("type")
#Expose
private String type;
#SerializedName("version")
#Expose
private String version;
#SerializedName("id")
#Expose
private String id;
#SerializedName("speak")
#Expose
private String speak;
#SerializedName("body")
#Expose
private List<Body> body = null;
#SerializedName("actions")
#Expose
private List<Action> actions = null;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getSpeak() {
return speak;
}
public void setSpeak(String speak) {
this.speak = speak;
}
public List<Body> getBody() {
return body;
}
public void setBody(List<Body> body) {
this.body = body;
}
public List<Action> getActions() {
return actions;
}
public void setActions(List<Action> actions) {
this.actions = actions;
}
}
Item.java
package com.example;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Item {
#SerializedName("type")
#Expose
private String type;
#SerializedName("columns")
#Expose
private List<Column> columns = null;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public List<Column> getColumns() {
return columns;
}
public void setColumns(List<Column> columns) {
this.columns = columns;
}
}
Item_.java also here better integration needed
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Item_ {
#SerializedName("type")
#Expose
private String type;
#SerializedName("size")
#Expose
private String size;
#SerializedName("weight")
#Expose
private String weight;
#SerializedName("text")
#Expose
private String text;
#SerializedName("wrap")
#Expose
private Boolean wrap;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
public String getWeight() {
return weight;
}
public void setWeight(String weight) {
this.weight = weight;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Boolean getWrap() {
return wrap;
}
public void setWrap(Boolean wrap) {
this.wrap = wrap;
}
}
hope it helps!
I've figured out this solution via the following solution:
JSONObject and JSONArray
JSONArray body = contentObj.getJSONArray("body");
JSONArray actions = contentObj.getJSONArray("actions");
title.setText(body.getJSONObject(0).getString("text"));
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 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());
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.