Jackson JSON converted bean, bean does not have the corresponding value Jackson unrecognized Field

Source: Internet
Author: User

I use Jackson for converting JSON to object class.

JSON:

{ "aaa":"111", "bbb":"222", "ccc":"333" }

Object class:

class Test{    public String aaa;    public String bbb;}

Code:

ObjectMapper mapper = new ObjectMapper();Object obj = mapper.readValue(content, valueType);

My code throws exception like that: org. codehaus. Jackson. Map. exc. unrecognizedpropertyexception: Unrecognized field "CCCC" (class criteria. Result. getgoodsinforesult), not marked as ignorable

And I don't want to add a prop to class test, I just want Jackson convert the exist value whith is also exist in test.

 

 

 

 

Jackson provides a few different mechanisms to configure handling of "extra" JSON elements. Following is an example of encryption ing
ObjectMapperTo notFAIL_ON_UNKNOWN_PROPERTIES.

import org.codehaus.jackson.annotate.JsonAutoDetect.Visibility;import org.codehaus.jackson.annotate.JsonMethod;import org.codehaus.jackson.map.DeserializationConfig;import org.codehaus.jackson.map.ObjectMapper;public class JacksonFoo{  public static void main(String[] args) throws Exception  {    // { "aaa":"111", "bbb":"222", "ccc":"333" }    String jsonInput = "{ \"aaa\":\"111\", \"bbb\":\"222\", \"ccc\":\"333\" }";    ObjectMapper mapper = new ObjectMapper().setVisibility(JsonMethod.FIELD, Visibility.ANY);    mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);    Test test = mapper.readValue(jsonInput, Test.class);  }}class Test{  String aaa;  String bbb;}

For other approaches, see

Http://wiki.fasterxml.com/JacksonHowToIgnoreUnknown

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.