Java Jackson无法识别存在的字段
这是我的JSONJava Jackson无法识别存在的字段,java,json,jackson,Java,Json,Jackson,这是我的JSON {"totalSize":46,"done":true,"records":[{"Name":"Wamu I","Start_Date__c":"2016-09-26T16:56:10.000+0000","Status__c":"Completed","Type__c":"Your were expecting success, but In reality it was I, Dio!!!"}]} 下面是我的两个实体类: @JsonIgnoreProperties(ign
{"totalSize":46,"done":true,"records":[{"Name":"Wamu I","Start_Date__c":"2016-09-26T16:56:10.000+0000","Status__c":"Completed","Type__c":"Your were expecting success, but In reality it was I, Dio!!!"}]}
下面是我的两个实体类:
@JsonIgnoreProperties(ignoreUnknown = true)
public class EsidesiJobEntity {
@JsonProperty("totalSize")
private @Getter @Setter Integer totalSize;
@JsonProperty("done")
private @Getter @Setter Boolean isDone;
@JsonProperty("records")
private @Getter @Setter List<KarsEntity> records;
@Override
@JsonIgnore
public String toString(){
List<String> recordsObjectString = new ArrayList<String>();
this.records.forEach((record) ->
{
recordsObjectString.add(record.toString());
});
return "{ totalSize:"+this.totalSize+", isDone:"+this.isDone+", records:["+recordsObjectString.toString()+"]";
}
}
@JsonIgnoreProperties(ignoreUnknown = true)
public class KarsEntity {
@JsonProperty("Name")
private @Getter @Setter String name;
@JsonProperty("Start_Date__c")
private @Getter @Setter String startDate;
@JsonProperty("Status__c")
private @Getter @Setter String status;
@Override
public String toString(){
return "{ name:"+this.name+", startDate:"+this.startDate+", status:"+this.status+"}";
}
}
但它肯定存在于JSON和实体中
下面是我编写的代码,用于将字符串映射到实体以供参考:
EsidesiEntity apexJobResponseEntity;
ObjectMapper apexMapper = new ObjectMapper();
try {
apexJobResponseEntity = apexMapper.readValue(apexResponseString, EsidesiEntity.class);
} ...
我是不是错过了一些基本的东西
(顺便说一句,如果类/实体名称中存在一些不一致,那是因为我在将它们发布到网上之前对它们进行了重命名。请告诉我,我会在看到它们时修复它们。)
谢谢 您正在使用。Jackson看不到你的getter和setter方法
所以你有两个选择:
<dependency>
<groupId>com.xebia</groupId>
<artifactId>jackson-lombok</artifactId>
<version>1.1</version>
</dependency>
我相信您应该像下面这样做:apexJobResponse=apexapper.readValue(“您的json字符串”,EsidesiEntity.class);什么是apexResponse?这是您的json字符串吗?实际上,apexResponse是一个字符串,apexJobResponse是对象。让我更清楚一点,所以我决定采用非龙目路线(至少是为了让它工作),但也会出现同样的错误。我尝试在getter和setter.Hm上使用和不使用“@JsonProperty(…)”注释。好的,我确信没有必要对这些方法进行注释。你确定你使用的是最新的jackson版本吗?我假设因为方法名类似于java变量,而不是JSON属性,所以我也可以对它们进行注释。但是它不起作用。你能在github上准备一个小例子,或者用更多的代码更新你的问题吗?好的,我知道我做错了什么。您修复了其中一个问题(更大的问题)。另一个问题发生在我做了接球手和二传手之后。出于某种原因,Eclipse自动生成私有getter和setter。我修好了,它开始工作了。谢谢
<dependency>
<groupId>com.xebia</groupId>
<artifactId>jackson-lombok</artifactId>
<version>1.1</version>
</dependency>
ObjectMapper apexMapper = new ObjectMapper();
apexMapper.setAnnotationIntrospector(new JacksonLombokAnnotationIntrospector());
[...]