我正在尝试读取jsonObject并在我的实体中插入以创建一个新的实体。问题是我得到application = null和version = 0并且不是我在json中的正确数据。
这是我尝试使用GSON库的代码:
final Gson gson = new Gson();
VersionDTO jsonObject = gson.fromJson(entity, VersionDTO.class);
try {
versionFilterService.setVersionDTO(jsonObject);
} catch (ServiceException e) {
e.printStackTrace();
}
entity是一个让我的json具有以下格式的String:
{
"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
}
}
这是我的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;
}
}
答案 0 :(得分:2)
您必须将VersionDTO
更改为Request
属性。 Request
属性应定义应用程序和版本属性。
您可以使用此tool为JSON创建POJO。
-----------------------------------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;
}
}