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 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 is also exist in Test.

 

 

 

 

Jackson provides a few different mechanisms to configure handling of "extra" JSON elements.  Following is an 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":"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

 

GitHub 加速计划 / js / json
41.72 K
6.61 K
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:1 个月前 )
960b763e 3 个月前
8c391e04 6 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐