How do I consume an array of JSON objects with Spring Boot? - java

With reference to this guide:
https://spring.io/guides/gs/consuming-rest/
The guide shows how to consume a RESTful web service.
The response from the REST API query results in the following JSON:
{
type: "success",
value: {
id: 10,
quote: "Really loving Spring Boot, makes stand alone Spring apps easy."
}
}
It creates a domain class called Quote.java to contain the data in the response:
package hello;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
#JsonIgnoreProperties(ignoreUnknown = true)
public class Quote {
private String type;
private Value value;
public Quote() {
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Value getValue() {
return value;
}
public void setValue(Value value) {
this.value = value;
}
#Override
public String toString() {
return "Quote{" +
"type='" + type + '\'' +
", value=" + value +
'}';
}
}
My questions is how do I represent the following json:
{
"size": 1,
"limit": 25,
"isLastPage": true,
"values": [
{
"user": {
"name": "jcitizen",
"emailAddress": "jane#example.com",
"id": 101,
"displayName": "Jane Citizen",
"active": true,
"slug": "jcitizen",
"type": "NORMAL"
},
"permission": "ADMIN"
}
],
"start": 0
}
The outer objects like size and limit are straightforward but I can't figure out how to represent the values object, which looks like an array of json objects.

This should work.
class Output {
private String size,
private int limit;
private boolean isLastPage,
private List<Value> values;
private int start ;
}
class Value
{
User user,
private String permission;
}
class User {
private String name,
private String emailAddress,
private int id,
private String displayName,
private boolean active,
private String slug,
private String type
}

Related

rest api - PUT method does not consume the data that GET method fetches

I obtain data with GET method and try to fed them to PUT method.
And I get a bad request error.
But when I edit the JSON as below then everithing works fine.
So why does the first JSON not work?
Controller class:
#RestController
#RequestMapping("/api")
public class FileController {
private final FileService fileService;
private final DocService docService;
private final DraftFileToPresentationConverter draftFileToPresentationConverter;
#Autowired
private DocFileRelationRepository docFileRelationRepository;
#Autowired
public FileController(FileService fileService,
DocService docService,
DraftFileToPresentationConverter draftFileToPresentationConverter) {
this.fileService = fileService;
this.docService = docService;
this.draftFileToPresentationConverter = draftFileToPresentationConverter;
}
#GetMapping("/docs/files/{id}")
public ResponseEntity<FilePresentation> getDraftFile(#PathVariable Long id) {
DraftFile file = fileService.getFile(id);
FilePresentation filePresentation = draftFileToPresentationConverter.convert(file);
return new ResponseEntity<>(filePresentation, HttpStatus.OK);
}
#PutMapping("/docs/files/{id}")
public ResponseEntity<FilePresentation> updateDraftFile(#RequestBody FilePresentation filePresentation) {
fileService.update(draftFileToPresentationConverter.convert(filePresentation));
return new ResponseEntity<>(filePresentation, HttpStatus.OK);
}
DTO:
#Data
public class FilePresentation {
private Long id;
private States state;
private String name;
private String user;
private Date dateUpload;
private Long lenght;
private IdRef document;
private IdRef judgeDoc;
public String getSize()
{
Double result = Double.valueOf(lenght/1024.0/1024.0);
if(result<1)
{
result = Double.valueOf(lenght/1024.0);
if(result<1)
{
return (lenght + " байт");
}
return (result.intValue() + " Кбайт");
}
return (result.intValue() + " Мбайт");
}
}
Troublesome class:
#Data
public class IdRef {
public IdRef(Long id) {
this.id = id;
}
private Long id;
}
JSON that I get with GET method and try to fed to PUT method (and get 400 Bad Request):
{
"id": 21,
"state": "DRAFT",
"name": "DNS-list.tiff",
"user": "JSmith",
"dateUpload": null,
"lenght": 28,
"document": {
"id": 141
},
"judgeDoc": null,
"size": "28 байт"
}
JSON that DOES work
{
"id": 21,
"state": "DRAFT",
"name": "DNS-list.tiff",
"user": "JSmith",
"dateUpload": null,
"lenght": 28,
"document": 141,
"judgeDoc": null,
"size": "28 байт"
}
Try to update RequestBody with #PathVariable
The constructor in IdRef was the reason.
I removed the constructor and it works fine now, my controller consumes the first JSON without errors.

An Attribute in JSON response can be of more than one type

I am making an Android native app that makes requests Using Retrofit2 to the WooCommerce API and gets the JSON responses mapping them with Model Classes (POJO) with the help of GSON. I use a plugin in Android Studio that generates the POJOs from JSON responses automatically.
When sending requests to the WC API, some end-points have this kind of responses:
"meta_data": [
{
"id": 2881,
"key": "wc_productdata_options",
"value": [
{
"_bubble_new": "\"yes\"",
"_bubble_text": "معجون أسنان",
"_custom_tab_title": "معجون أسنان Oral-B",
"_custom_tab": "",
"_product_video": "",
"_product_video_size": "",
"_product_video_placement": "",
"_top_content": "",
"_bottom_content": ""
}
]
},
{
"id": 3077,
"key": "_wp_page_template",
"value": "default"
}
]
The attribute value can be either a String or a List<Value> but the POJO plugin defines value as only List<Value>
So when parsing the response I get an error that GSON expects BEGIN_ARRAY but got STRING instead when it reaches "value" : "default".
How can I represent that value can be a string OR a list in the Model Class.
Here's the automatically generated POJO of meta_data
import com.google.gson.annotations.SerializedName;
import java.util.List;
public class MetaDatum {
#SerializedName("id")
private Long mId;
#SerializedName("key")
private String mKey;
#SerializedName("value")
private List<Value> mValue;
public Long getId() {
return mId;
}
public void setId(Long id) {
mId = id;
}
public String getKey() {
return mKey;
}
public void setKey(String key) {
mKey = key;
}
public List<Value> getValue() {
return mValue;
}
public void setValue(List<Value> value) {
mValue = value;
}
}
I defined value as of type Object so it can be cast somehow to whatever comes in the JSON. It worked!

JSON Array of objects to plain old java object

this is my first time making an external api call in Java, so please bear with me as I'm not very experienced. I got the http request working and got a response, but now I need to parse it.
I'm trying to convert a json array to java objects. I understand the gist of it, but all examples I've seen don't apply to my issue.
I need the 'entities' objects from the json string. The details (which are an array, too) can contain any key/value pair, so I was thinking of putting that in a hashmap in each Entity object. I've tried the gson library, but I can't find any gson example that goes deeper than a single dimensional json array.
I realize this is kind of a broad question, and I don't expect anyone to deliver me a working solution, but a few tips or a link to a relevant guide would go a long way. :)
{
"return": {
"entities": [
{
"id": 2385,
"details": [
{
"name": "Other Known Name",
"value": "John Wick",
"match": false
}
],
"proofs": [],
"link": "http://domain.gg/users?id=2385"
},
{
"id": 2384,
"details": [
{
"name": "Discord ID",
"value": "159985870458322944",
"match": false
},
{
"name": "SteamID64",
"value": "76561197991558078",
"match": true
},
{
"name": "SteamVanity",
"value": "test",
"match": false
},
{
"name": "PS4",
"value": "John_S",
"match": false
},
{
"name": "XBox",
"value": "John S",
"match": false
},
{
"name": "Email",
"value": "john_smith#gmail.com",
"match": true
},
{
"name": "Comment",
"value": "Test user",
"match": false
},
{
"name": "Other Known Name",
"value": "Jonathan",
"match": false
},
{
"name": "Reddit",
"value": "/u/johns",
"match": true
}
],
"proofs": [],
"link": "http://domain.gg/users?id=2384"
},
{
"id": 1680,
"details": [
{
"name": "Other Known Name",
"value": "Johny",
"match": false
},
{
"name": "SteamID64",
"value": "76561198213003675",
"match": true
}
],
"proofs": [],
"link": "http://domain.gg/users?id=1680"
},
{
"id": 1689,
"details": [
{
"name": "Other Known Name",
"value": "JohnnyPeto",
"match": false
},
{
"name": "SteamID64",
"value": "76561198094228192",
"match": true
}
],
"proofs": [],
"link": "http://domain.gg/users?id=1689"
}
],
"notice": "Showing 4 out of 4 matches."
}
}
There are many json serialization/deserialization frameworks available. I would recommend having a look at Jackson.
Basically, you have to create Model corresponding to json schema and deserialize json into object. Based on the example in the question, model will look like this:
#JsonIgnoreProperties(ignoreUnknown = true)
class Response {
#JsonProperty("return")
private ResponseObject responseObject;
public ResponseObject getResponseObject() {
return responseObject;
}
public void setResponseObject(ResponseObject responseObject) {
this.responseObject = responseObject;
}
}
#JsonIgnoreProperties(ignoreUnknown = true)
class ResponseObject {
private List<Entity> entities;
public List<Entity> getEntities() {
return entities;
}
public void setEntities(List<Entity> entities) {
this.entities = entities;
}
}
#JsonIgnoreProperties(ignoreUnknown = true)
class Entity {
private String id;
private List<Details> details;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public List<Details> getDetails() {
return details;
}
public void setDetails(List<Details> details) {
this.details = details;
}
}
#JsonIgnoreProperties(ignoreUnknown = true)
class Details {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Once the model is defined, you can use ObjectMapper class to perform serialization/deserialization, e.g.:
ObjectMapper mapper = new ObjectMapper();
Response response = mapper.readValue("{\"return\": {\"entities\": [{\"id\": 2385,\"details\": [{\"name\": \"Other Known Name\",\"value\": \"John Wick\",\"match\": false}],\"proofs\": [],\"link\": \"http://domain.gg/users?id=2385\"},{\"id\": 2384,\"details\": [{\"name\": \"Discord ID\",\"value\": \"159985870458322944\",\"match\": false},{\"name\": \"SteamID64\",\"value\": \"76561197991558078\",\"match\": true},{\"name\": \"SteamVanity\",\"value\": \"test\",\"match\": false},{\"name\": \"PS4\",\"value\": \"John_S\",\"match\": false},{\"name\": \"XBox\",\"value\": \"John S\",\"match\": false},{\"name\": \"Email\",\"value\": \"john_smith#gmail.com\",\"match\": true},{\"name\": \"Comment\",\"value\": \"Test user\",\"match\": false},{\"name\": \"Other Known Name\",\"value\": \"Jonathan\",\"match\": false},{\"name\": \"Reddit\",\"value\": \"/u/johns\",\"match\": true}],\"proofs\": [],\"link\": \"http://domain.gg/users?id=2384\"},{\"id\": 1680,\"details\": [{\"name\": \"Other Known Name\",\"value\": \"Johny\",\"match\": false},{\"name\": \"SteamID64\",\"value\": \"76561198213003675\",\"match\": true}],\"proofs\": [],\"link\": \"http://domain.gg/users?id=1680\"},{\"id\": 1689,\"details\": [{\"name\": \"Other Known Name\",\"value\": \"JohnnyPeto\",\"match\": false},{\"name\": \"SteamID64\",\"value\": \"76561198094228192\",\"match\": true}],\"proofs\": [],\"link\": \"http://domain.gg/users?id=1689\"}],\"notice\": \"Showing 4 out of 4 matches.\"}}", Response.class);
System.out.println(response.getResponseObject().getEntities().get(0).getId());
Here's the Javadoc.
If I were you, I'd use Jackson, not GSON. It's specialized on JavaBeans-style mapping. Write classes like this:
public class Detail{
private String name;
private String value;
private boolean match;
// + getters / setters
}
public class Entity{
private int id;
private List<Detail> details;
private String link;
private List<String> proofs;
// you don't have any example data for this, so I'm assuming strings
// + getters / setters
}
public class Result{
private List<Entity> entities;
private String notice;
// + getters / setters
}
and do the conversion with something like
Result result = new ObjectMapper().readValue(json, Result.class);
As my fellow stackoverflow users have previously posted, for this kind of initilization Jackson API would be better. I have however posted the solution for your question with Gson.
I noticed that you like your details to be stored as a HashMap with id as key. However, it seems like this id is actually related to the entities and not to the details.
Disclaimer, I got lazy and used an online POJO generator because I did not want to create objects for all of the Json elements ;) It still showcases how it should be done:
class Main{
public static void main(String[] args) throws FileNotFoundException {
//this is just to load the json file
String input = new Scanner(new File("test.txt")).useDelimiter("\\Z").next();
System.out.println(input);
Gson gson = new Gson();
Example arr = gson.fromJson(input, Example.class);
System.out.println(arr);
}
public class Detail {
#SerializedName("name")
#Expose
public String name;
#SerializedName("value")
#Expose
public String value;
#SerializedName("match")
#Expose
public Boolean match;
#Override
public String toString() {
return "Detail [name=" + name + ", value=" + value + ", match=" + match + "]";
}
}
public class Entity {
#SerializedName("id")
#Expose
public Integer id;
#SerializedName("details")
#Expose
public List<Detail> details = null;
#SerializedName("proofs")
#Expose
public List<Object> proofs = null;
#SerializedName("link")
#Expose
public String link;
#Override
public String toString() {
return "Entity [id=" + id + ", details=" + details + ", proofs=" + proofs + ", link=" + link + "]";
}
}
public class Example {
#SerializedName("return")
#Expose
public Return _return;
#Override
public String toString() {
return "Example [_return=" + _return + "]";
}
}
public class Return {
#SerializedName("entities")
#Expose
public List<Entity> entities = null;
#SerializedName("notice")
#Expose
public String notice;
#Override
public String toString() {
return "Return [entities=" + entities + ", notice=" + notice + "]";
}
}
}
Output
Example [_return=Return [entities=[Entity [id=2385, details=[Detail [name=Other Known Name, value=John Wick, match=false]], proofs=[], link=http://domain.gg/users?id=2385], Entity [id=2384, details=[Detail [name=Discord ID, value=159985870458322944, match=false], Detail [name=SteamID64, value=76561197991558078, match=true], Detail [name=SteamVanity, value=test, match=false], Detail [name=PS4, value=John_S, match=false], Detail [name=XBox, value=John S, match=false], Detail [name=Email, value=john_smith#gmail.com, match=true], Detail [name=Comment, value=Test user, match=false], Detail [name=Other Known Name, value=Jonathan, match=false], Detail [name=Reddit, value=/u/johns, match=true]], proofs=[], link=http://domain.gg/users?id=2384], Entity [id=1680, details=[Detail [name=Other Known Name, value=Johny, match=false], Detail [name=SteamID64, value=76561198213003675, match=true]], proofs=[], link=http://domain.gg/users?id=1680], Entity [id=1689, details=[Detail [name=Other Known Name, value=JohnnyPeto, match=false], Detail [name=SteamID64, value=76561198094228192, match=true]], proofs=[], link=http://domain.gg/users?id=1689]], notice=Showing 4 out of 4 matches.]]
Despite there are answers suggesting you to use Jackson, you can still accomplish easily with Gson with its default configuration just creating proper relations between mappings:
// A generic response, parameterized with <T>, can hold any type except of primitives
final class Response<T> {
#SerializedName("return")
final T ret = null;
}
final class EntitiesAndNotice {
final List<Entity> entities = null;
final String notice = null;
}
final class Entity {
// Unlike Object and any its subclasses, `int` being a primitive cannot be nulled
// Simple 0 won't work either, because the compiler will inline it
// So it's a sort of cheating javac to return a value that holds 0 already
final int id = Integer.valueOf(0);
final List<Detail> details = null;
// Your JSON document does not provide enough info on the elements type
// So it depends on how Gson parses JSON tokens
final List<Object> proofs = null;
final URL link = null;
}
final class Detail {
final String name = null;
final String value = null;
// The same for primitive booleans, or Boolean.FALSE
final boolean match = Boolean.valueOf(false);
}
Example use:
private static final String JSON = "{\"return\":{\"entities\":[{\"id\":2385,\"details\":[{\"name\":\"Other Known Name\",\"value\":\"John Wick\",\"match\":false}],\"proofs\":[],\"link\":\"http://domain.gg/users?id=2385\"},{\"id\":2384,\"details\":[{\"name\":\"Discord ID\",\"value\":\"159985870458322944\",\"match\":false},{\"name\":\"SteamID64\",\"value\":\"76561197991558078\",\"match\":true},{\"name\":\"SteamVanity\",\"value\":\"test\",\"match\":false},{\"name\":\"PS4\",\"value\":\"John_S\",\"match\":false},{\"name\":\"XBox\",\"value\":\"John S\",\"match\":false},{\"name\":\"Email\",\"value\":\"john_smith#gmail.com\",\"match\":true},{\"name\":\"Comment\",\"value\":\"Test user\",\"match\":false},{\"name\":\"Other Known Name\",\"value\":\"Jonathan\",\"match\":false},{\"name\":\"Reddit\",\"value\":\"/u/johns\",\"match\":true}],\"proofs\":[],\"link\":\"http://domain.gg/users?id=2384\"},{\"id\":1680,\"details\":[{\"name\":\"Other Known Name\",\"value\":\"Johny\",\"match\":false},{\"name\":\"SteamID64\",\"value\":\"76561198213003675\",\"match\":true}],\"proofs\":[],\"link\":\"http://domain.gg/users?id=1680\"},{\"id\":1689,\"details\":[{\"name\":\"Other Known Name\",\"value\":\"JohnnyPeto\",\"match\":false},{\"name\":\"SteamID64\",\"value\":\"76561198094228192\",\"match\":true}],\"proofs\":[],\"link\":\"http://domain.gg/users?id=1689\"}],\"notice\":\"Showing 4 out of 4 matches.\"}}";
private static final Gson gson = new Gson();
private static final TypeToken<Response<EntitiesAndNotice>> responseTypeToken = new TypeToken<Response<EntitiesAndNotice>>() {
};
public static void main(final String... args) {
final Response<EntitiesAndNotice> response = gson.fromJson(JSON, responseTypeToken.getType());
final String value = response.ret.entities.get(1).details.get(3).value;
System.out.println(value);
}
Output:
John_S

Convert a JSON String into Java Object

i have a String returned by a service, in this JSON format:
String message = {
"Tickets":
[{
"Type": "type1",
"Author": "author1",
"Rows":
[
{
"Price": "100.0",
"Date": "24/06/2016",
"Amount": "10"
},
{
"Type": "Comment",
"Value": "some comment goes here"
}
],
"ID": "165"
}],
"Desk": "desk1",
"User": "user1"
}
I need to parse it and convert into a Java object.
I tried to create a dom like this:
public class TicketWrapper{
private Ticket ticket;
private String desk;
private String user;
}
public class Ticket {
private String type;
private String author;
private List<Row> rows;
private String id;
}
public class Row1{
private float price;
private Date date;
private int amount;
}
public class Row2{
private String type;
private float value;
}
Then I try to parse it with Google Gson, this way:
TicketWrapper ticket = gson.fromJson(message, TicketWrapper.class)
but if I print it System.out.println(gson.toJson(ticket)), it prints:
{"desk" : 0, "user" : 0}
I don't know how to parse that Json into a Java Object, and how to tell him that a row into "Rows" can be of the Row1 type or Row2 type.
I think there is a few issues, such names of properties in lower case and dateformat and mix types of rows. I just changed like this and worked for me:
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.SerializedName;
import org.junit.Test;
import java.util.Date;
import java.util.List;
public class CheckTest {
#Test
public void thisTest() {
Gson gson = new GsonBuilder()
.setDateFormat("dd-MM-yyyy")
.setPrettyPrinting()
.create();
String message = "{\"Tickets\":" +
"[{\"Type\":\"type1\"," +
"\"Author\":\"author1\"," +
"\"Rows\":[{\"Price\":\"100.0\"," +
"\"Date\":\"24-06-2016\"," +
"\"Amount\":\"10\"}," +
"{\"Type\":\"Comment\"," +
"\"Value\":\"some comment goes here\"}]," +
"\"ID\":\"165\"}]," +
"\"Desk\":\"desk1\"," +
"\"User\":\"user1\"}";
TicketWrapper ticket = gson.fromJson(message, TicketWrapper.class);
System.out.println(ticket.toString());
}
public class TicketWrapper {
#SerializedName("Tickets")
private List<Ticket> tickets;
#SerializedName("Desk")
private String desk;
#SerializedName("User")
private String user;
public TicketWrapper() {
}
}
public class Ticket {
#SerializedName("Type")
private String type;
#SerializedName("Author")
private String author;
#SerializedName("Rows")
private List<Row> rows;
#SerializedName("ID")
private String id;
public Ticket() {
}
}
public class Row {
#SerializedName("Type")
private String type;
#SerializedName("Value")
private String value;
#SerializedName("Price")
private float price;
#SerializedName("Date")
private Date date;
#SerializedName("Amount")
private int amount;
public Row() {
}
}
}
As others have already mentioned in the comment, you need to make sure the mapping directly reflects the file names. It needs to be 'User' and 'Desk' instead of 'user' and 'desk'. Also, you have a list of tickets, which would map to List Tickets.

Android: JSON to objects using GSON fails

I’m a newbee to Android development and have a question about getting a JSON string transformed to class instances using GSON, version 2.6.1.
I have a need for transforming a string like this to objects:
{
"Messages": [{
"ForwardMsg": true,
"IsAdmin": true,
"MsgBody": "Some text",
"SysInfo": null,
"Recipients": ["Some test"]
}, {
"ForwardMsg": true,
"IsAdmin": false,
"MsgBody": "Some other text",
"SysInfo": null,
"Recipients": ["Some test", "Some more text"]
}]
}
With this (http://howtodoinjava.com/best-practices/google-gson-tutorial-convert-java-object-to-from-json/ ) as inspiration, I’ve come up with the following:
I have a class DemoMessageList that looks like this:
import java.util.List;
public class DemoMessageList {
private List< DemoMessage> messages;
public DemoMessageList () {
}
public List< DemoMessage > getMessages() {
return messages;
}
public void setMessages(List< DemoMessage > messages) {
this.messages = messages;
}
#Override
public String toString()
{
return "Messages ["+ messages + "]";
}
}
And a class DemoMessage that looks like this:
import java.util.List;
public class DemoMessage {
private Boolean forwardMsg;
private Boolean isAdmin;
private String msgBody;
private String sysInfo;
private List<String> recipients;
public Boolean getForwardMsg() {
return forwardMsg;
}
public void setForwardMsg(Boolean forwardMsg) {
this.forwardMsg = forwardMsg;
}
public Boolean doForwardMsg() {
return forwardMsg;
}
public Boolean getIsAdmin() {
return isAdmin;
}
public void setIsAdmin(Boolean isAdmin) {
this.isAdmin = isAdmin;
}
public String getMsgBody() {
return msgBody;
}
public void setMsgBody(String msgBody) {
this.msgBody = msgBody;
}
public String getSysInfo() {
return sysInfo;
}
public void setSysInfo(String sysInfo) {
this.sysInfo = sysInfo;
}
public List<String> getRecipients() {
return recipients;
}
public void setRecipients(List<String> recipients) {
this.recipients = recipients;
}
}
When I do this, to try transform:
public void test() {
String demoData = {"Messages": [{ "ForwardMsg": true, "IsAdmin": false,"MsgBody": "Some other text", "SysInfo": null, "Recipients": ["Some test", "Some more text"]}]}
Log.d("AsData ", "demoData: " + demoData);
Gson gson = new Gson();
DemoMessageList dmList = gson.fromJson(demoData, DemoMessageList.class);
Log.d("AsList ", "dmList: " + dmList.toString());
Log.d("ListSize ", "dmList - Size: " + String.valueOf(dmList.getMessages().size()));
}
I get this logged:
demoData: {"Messages": [{ "ForwardMsg": true, "IsAdmin": false, "MsgBody": "Some other text", "SysInfo": null, "Recipients": ["Some test", "Some more text"]}]}
dmList: Messages [null]
dmList - Size: 0
Why does this fail??
Please help!!!
Your JSON names are different from your class field names. GSON looks at your field names for transformation.
Use your model class like this with custom naming,
#SerializedName("ForwardMsg")
private Boolean forwardMsg;
#SerializedName("IsAdmin")
private Boolean isAdmin;
#SerializedName("MsgBody")
private String msgBody;
#SerializedName("SysInfo")
private String sysInfo;
#SerializedName("Recipients")
private List<String> recipients;
and keep your other class ,
#SerializedName("Messages")
private List< DemoMessage> messages;
Use camel case on your JSON property names:
{
"messages": [{
"forwardMsg": true,
"isAdmin": true,
"msgBody": "Some text",
"sysInfo": null,
"recipients": ["Some test"]
}, {
"forwardMsg": true,
"isAdmin": false,
"msgBody": "Some other text",
"sysInfo": null,
"recipients": ["Some test", "Some more text"]
}]
}
.. and make the fieldnames match the case of the JSON property names, e.g.:
private List<DemoMessage> messages;
In short: The JSON property names must match the fields defined in your class(es), both by spelling and letter case.
You rock guys!!
Bharath Mg' answer works perfect in my little test, when adding this:
import com.google.gson.annotations.SerializedName;
I have no control over the string containing the JSON in the real world application as it is provided by a webservice.
This has been bugging me for the 2 days, so I look forward to get on with project.
Thanks again
Bharat's answer is correct. Fields names are case-sensitive.

Categories