java json not marked as ignorable,杰克逊与JSON:无法识别的字段,未标记为可忽略

I need to convert a certain JSON string to a Java object. I am using Jackson for JSON handling. I have no control over the input JSON (I read from a web service). This is my input JSON:

{"wrapper":[{"id":"13","name":"Fred"}]}

Here is a simplified use case:

private void tryReading() {

String jsonStr = "{\"wrapper\"\:[{\"id\":\"13\",\"name\":\"Fred\"}]}";

ObjectMapper mapper = new ObjectMapper();

Wrapper wrapper = null;

try {

wrapper = mapper.readValue(jsonStr , Wrapper.class);

} catch (Exception e) {

e.printStackTrace();

}

System.out.println("wrapper = " + wrapper);

}

My entity class is:

public Class Student {

private String name;

private String id;

//getters & setters for name & id here

}

My Wrapper class is basically a container object to get my list of students:

public Class Wrapper {

private List students;

//getters & setters here

}

I keep getting this error and "wrapper" returns null. I am not sure what's missing. Can someone help please?

org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "wrapper" (Class Wrapper), not marked as ignorable

at [Source: java.io.StringReader@1198891; line: 1, column: 13] (through reference chain: Wrapper["wrapper"])

at org.codehaus.jackson.map.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:53)

解决方案

You can use Jackson's class-level annotation:

@JsonIgnoreProperties

It will ignore every property you haven't defined in your POJO. Very useful when you are just looking for a couple of properties in the JSON and don't want to write the whole mapping. More info at Jackson's website. If you want to ignore any non declared property, you should write:

@JsonIgnoreProperties(ignoreUnknown = true)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值