【问题标题】:Reading GSON object is getting null elements读取 GSON 对象正在获取空元素
【发布时间】:2016-11-28 11:22:02
【问题描述】:

我正在尝试读取一个 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 格式如下:

{
    "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;
    }

}

【问题讨论】:

  • 您自己可能会看到,您的versionDTO 类结构 与您获得的json 不匹配。因此,创建一个类 Foo,它有一个名为 request 的 VersionDTO 字段,并将 Foo 与 gson 一起使用
  • 我想要做的是创建一个对象,其中仅包含具有 VersionDTO 的元素。不知道我是否理解你的回答。请提供一小段代码好吗?

标签: java json spring gson


【解决方案1】:

您必须将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;
}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多