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