Jackson JSON conversion bean, no corresponding value in the Bean Jackson unrecognized FIELD__JS

Source: Internet
Author: User
Tags visibility

I use the Jackson for converting JSON to Object class.

Json:

{"AAA": "A", "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 com.isoftstone.banggo.net.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 was also exist in Test.

Jackson provides a few different mechanisms to configure handling of "extra" JSON elements. Following is a example of configuring the Objectmapper to not fail_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": "", "BB B ":" "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 the other approaches, 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.