Java Gson-fromJson返回空对象
我对一个愚蠢的错误感到沮丧,我试图将一个json解析到我想要的类,但不知何故它返回了一个空对象。我的json如下:Java Gson-fromJson返回空对象,java,json,gson,Java,Json,Gson,我对一个愚蠢的错误感到沮丧,我试图将一个json解析到我想要的类,但不知何故它返回了一个空对象。我的json如下: { "service_requests": [ { "serviceRequestItems": [ { "entityId": "688210985", "entityType": "wh_i
{
"service_requests": [
{
"serviceRequestItems": [
{
"entityId": "688210985",
"entityType": "wh_inventory",
"sourceArea": "transient",
"destinationArea": "store",
"srItemLabels": [
],
"attributes": [
{
"name": "wid",
"value": "test"
}
]
}
]
}
]
}
这是从API调用返回的,我已经从IDE的调试模式验证了这一点。
以下是我的课程
@JsonIgnoreProperties(ignoreUnknown = true)
public class PayloadDTO {
@JsonProperty("service_requests")
List<SRCreationRequest> serviceRequests;
public List<SRCreationRequest> getServiceRequests() {
return serviceRequests;
}
public void setServiceRequests(List<SRCreationRequest> serviceRequests) {
this.serviceRequests = serviceRequests;
}
@Override
public String toString() {
return "PayloadDTO{" + "service_requests=" + serviceRequests + '}';
}
}
public class SRCreationRequest {
private List<SRItemCreationRequest> serviceRequestItems;
public List<SRItemCreationRequest> getServiceRequestItems() {
return serviceRequestItems;
}
public void setServiceRequestItems(List<SRItemCreationRequest> serviceRequestItems) {
this.serviceRequestItems = serviceRequestItems;
}
@Override
public String toString() {
return "SRCreationRequest{" + "serviceRequestItems=" + serviceRequestItems + ", tenantId='" + tenantId + '\''
+ ", clientId='" + clientId + '\'' + ", facilityId='" + facilityId + '\'' + ", context='" + context + '\''
+ ", idempotenceKey='" + idempotenceKey + '\'' + ", shardInfo=" + shardInfo + ", apiContext='" + apiContext
+ '\'' + '}';
}
}
public class SRItemCreationRequest {
private String entityId;
private String entityType;
private SRItemContainer sourceContainer;
private String sourceArea;
private SRItemContainer destinationContainer;
private String destinationArea;
private List<SRItemLabel> srItemLabels;
private String groupId;
private List<SRItemAttribute> attributes;
public String getEntityId() {
return entityId;
}
public void setEntityId(String entityId) {
this.entityId = entityId;
}
public String getEntityType() {
return entityType;
}
public void setEntityType(String entityType) {
this.entityType = entityType;
}
public SRItemContainer getSourceContainer() {
return sourceContainer;
}
public void setSourceContainer(SRItemContainer sourceContainer) {
this.sourceContainer = sourceContainer;
}
public String getSourceArea() {
return sourceArea;
}
public void setSourceArea(String sourceArea) {
this.sourceArea = sourceArea;
}
public SRItemContainer getDestinationContainer() {
return destinationContainer;
}
public void setDestinationContainer(SRItemContainer destinationContainer) {
this.destinationContainer = destinationContainer;
}
public String getDestinationArea() {
return destinationArea;
}
public void setDestinationArea(String destinationArea) {
this.destinationArea = destinationArea;
}
public List<SRItemLabel> getSrItemLabels() {
return srItemLabels;
}
public void setSrItemLabels(List<SRItemLabel> srItemLabels) {
this.srItemLabels = srItemLabels;
}
public String getGroupId() {
return groupId;
}
public void setGroupId(String groupId) {
this.groupId = groupId;
}
public List<SRItemAttribute> getAttributes() {
return attributes;
}
public void setAttributes(List<SRItemAttribute> attributes) {
this.attributes = attributes;
}
@Override
public String toString() {
return "SRItemCreationRequest{" + "entityId='" + entityId + '\'' + ", entityType='" + entityType + '\''
+ ", sourceContainer=" + sourceContainer + ", sourceArea='" + sourceArea + '\'' + ", destinationContainer="
+ destinationContainer + ", destinationArea='" + destinationArea + '\'' + ", srItemLabels=" + srItemLabels
+ ", groupId='" + groupId + '\'' + ", attributes=" + attributes + '}';
}
}
由于我没有使用SRItemContainer并忽略未知属性,所以我假设这不应该是一个问题。
我正在解析如下
Gson gson = new Gson();
String jsonInString = gson.toJson(//getting from API here);
LOGGER.debug("jsonInString "+jsonInString); //as above
PayloadDTO serviceRequests = gson.fromJson(jsonInString, PayloadDTO.class);
有人知道我哪里出了问题吗?你检查过你的第二个名为
SRCreationRequest
的类了吗。它不也需要有注释吗?本部分:
私有列表serviceRequestItems代码>
我认为您需要为GSON添加注释,以便将其作为API响应模型的属性来读取。在属性@JsonProperty(“serviceRequestItems”)
对于这种模型生成,我总是使用在线工具。手动操作会在某个地方导致错误,对于复杂的模型来说很难获得
请点击此处查看本网站:
我已经生成了对java类pojo类的json响应
结果如下:
对于PayloadTo:
公共类付费下载到{
@SerializedName(“服务请求”)
@暴露
私有列表serviceRequests=null;
公共列表getServiceRequests(){
返回服务请求;
}
公共无效设置服务请求(列出服务请求){
this.serviceRequests=serviceRequests;
}
}
对于ServiceRequest:
公共类服务请求{
@SerializedName(“serviceRequestItems”)
@暴露
私有列表serviceRequestItems=null;
公共列表getServiceRequestItems(){
返回服务请求项;
}
公共void setServiceRequestItems(列出serviceRequestItems){
this.serviceRequestItems=serviceRequestItems;
}
}
对于ServiceRequestItem:
公共类ServiceRequestItem{
@SerializedName(“entityId”)
@暴露
私有字符串entityId;
@SerializedName(“entityType”)
@暴露
私有字符串entityType;
@SerializedName(“源区域”)
@暴露
私有字符串源区;
@SerializedName(“destinationArea”)
@暴露
私有字符串目的地区域;
@SerializedName(“srItemLabels”)
@暴露
私有列表srItemLabels=null;
@SerializedName(“属性”)
@暴露
private List尝试在中实现可序列化类PayloadDTO@BeshambherChaukhwan我不明白,你能详细说明一下吗?要在json和Java类之间进行转换,该类必须实现可序列化接口,以便能够将其转换为字节流。有关更详细的信息,请查看上面关于Serializable
是错误的。当您使用Gson时,您的DTO被注释为与Jackson一起使用。为什么您使用不同的库,并且您没有怀疑从完全不同的包导入类时出错?当然,您得到的是null
:Gson无法将JSON服务请求
映射到sServiceRequests
,因为它是在Gson中使用@SerializedName
完成的。或者使用JacksonObjectMapper
。@fluffy是对的,我使用了错误包中的类。(我之前使用的是ObjectMapper库)Gson无法将serviceRequests
映射到serviceRequests
。将属性命名为serviceRequests
解决了这个问题。这是我应该做的,感谢您为我指明了未来的正确资源。
Gson gson = new Gson();
String jsonInString = gson.toJson(//getting from API here);
LOGGER.debug("jsonInString "+jsonInString); //as above
PayloadDTO serviceRequests = gson.fromJson(jsonInString, PayloadDTO.class);