S.P. S.P. - 11 days ago 7
JSON Question

Reading GSON object is getting null elements

I'm trying to read a jsonObject and insert inside my entity to create a new one entity. The problem is that I'm getting application=null and version=0 and is not the correct data that I have in the json.

This is my code where I trying to use the GSON library:

final Gson gson = new Gson();
VersionDTO jsonObject = gson.fromJson(entity, VersionDTO.class);

try {
versionFilterService.setVersionDTO(jsonObject);
} catch (ServiceException e) {
e.printStackTrace();
}


entity is a String that have my json with this format:

{
"context": {
"location":{
"longt":0,
"lat":0,
"radius":0
},
"accessToken":"asd",
"notificationToken":"dasda",
"requestTime":11111111,
"application":"1 Android Mobile",
"version":1
},
"request":{
"application":"1 Android Mobile",
"version":1
}
}


This is my VersionDTO:

public class VersionDTO {
int version;
String application;

public VersionDTO(int version, String application){
this.version = version;
this.application = application;
}

public String getApplication() {
return application;
}

public void setApplication(String application) {
this.application = application;
}

public int getVersion() {
return version;
}

public void setVersion(int version) {
this.version = version;
}

}

Answer

You got to change the VersionDTO to have Request attribute. The Request attribute should have the application and version attributes defined.

You can use this tool to create POJO for JSON.

-----------------------------------com.example.Context.java-----------------------------------

package com.example;

import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

@Generated("org.jsonschema2pojo")
public class Context {

@SerializedName("location")
@Expose
private Location location;
@SerializedName("accessToken")
@Expose
private String accessToken;
@SerializedName("notificationToken")
@Expose
private String notificationToken;
@SerializedName("requestTime")
@Expose
private Integer requestTime;
@SerializedName("application")
@Expose
private String application;
@SerializedName("version")
@Expose
private Integer version;

/**
* 
* @return
* The location
*/
public Location getLocation() {
return location;
}

/**
* 
* @param location
* The location
*/
public void setLocation(Location location) {
this.location = location;
}

/**
* 
* @return
* The accessToken
*/
public String getAccessToken() {
return accessToken;
}

/**
* 
* @param accessToken
* The accessToken
*/
public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}

/**
* 
* @return
* The notificationToken
*/
public String getNotificationToken() {
return notificationToken;
}

/**
* 
* @param notificationToken
* The notificationToken
*/
public void setNotificationToken(String notificationToken) {
this.notificationToken = notificationToken;
}

/**
* 
* @return
* The requestTime
*/
public Integer getRequestTime() {
return requestTime;
}

/**
* 
* @param requestTime
* The requestTime
*/
public void setRequestTime(Integer requestTime) {
this.requestTime = requestTime;
}

/**
* 
* @return
* The application
*/
public String getApplication() {
return application;
}

/**
* 
* @param application
* The application
*/
public void setApplication(String application) {
this.application = application;
}

/**
* 
* @return
* The version
*/
public Integer getVersion() {
return version;
}

/**
* 
* @param version
* The version
*/
public void setVersion(Integer version) {
this.version = version;
}

}
-----------------------------------com.example.Location.java-----------------------------------

package com.example;

import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

@Generated("org.jsonschema2pojo")
public class Location {

@SerializedName("longt")
@Expose
private Integer longt;
@SerializedName("lat")
@Expose
private Integer lat;
@SerializedName("radius")
@Expose
private Integer radius;

/**
* 
* @return
* The longt
*/
public Integer getLongt() {
return longt;
}

/**
* 
* @param longt
* The longt
*/
public void setLongt(Integer longt) {
this.longt = longt;
}

/**
* 
* @return
* The lat
*/
public Integer getLat() {
return lat;
}

/**
* 
* @param lat
* The lat
*/
public void setLat(Integer lat) {
this.lat = lat;
}

/**
* 
* @return
* The radius
*/
public Integer getRadius() {
return radius;
}

/**
* 
* @param radius
* The radius
*/
public void setRadius(Integer radius) {
this.radius = radius;
}

}
-----------------------------------com.example.Request.java-----------------------------------

package com.example;

import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

@Generated("org.jsonschema2pojo")
public class Request {

@SerializedName("application")
@Expose
private String application;
@SerializedName("version")
@Expose
private Integer version;

/**
* 
* @return
* The application
*/
public String getApplication() {
return application;
}

/**
* 
* @param application
* The application
*/
public void setApplication(String application) {
this.application = application;
}

/**
* 
* @return
* The version
*/
public Integer getVersion() {
return version;
}

/**
* 
* @param version
* The version
*/
public void setVersion(Integer version) {
this.version = version;
}

}
-----------------------------------com.example.VersionDTO.java-----------------------------------

package com.example;

import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

@Generated("org.jsonschema2pojo")
public class VersionDTO {

@SerializedName("context")
@Expose
private Context context;
@SerializedName("request")
@Expose
private Request request;

/**
* 
* @return
* The context
*/
public Context getContext() {
return context;
}

/**
* 
* @param context
* The context
*/
public void setContext(Context context) {
this.context = context;
}

/**
* 
* @return
* The request
*/
public Request getRequest() {
return request;
}

/**
* 
* @param request
* The request
*/
public void setRequest(Request request) {
this.request = request;
}

}