2016-11-28 90 views
0

我正在尝试读取jsonObject并在我的实体中插入以创建一个新的实体。问题是我得到应用程序= null和版本= 0,并不是我在json中的正确数据。阅读GSON对象获取null元素

这是我的代码,我尝试使用GSON库:

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

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

实体是有我的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; 
    } 

} 
+1

正如你可能自己看到的,你的versionDTO类结构**不匹配你得到的json。因此,创建一个具有名为'request'的VersionDTO字段的类Foo,并使用foo与gson – 2016-11-28 11:31:25

+0

我想要做的是创建一个只包含具有VersionDTO的元素的对象。不知道我是否理解你的答案。你能提供一小段代码吗? –

回答

2

你必须改变VersionDTORequest属性ibute。 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; 
} 

}