杰克逊杰森类型映射内部类 [英] Jackson Json Type Mapping Inner Class

查看:136
本文介绍了杰克逊杰森类型映射内部类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为作为JSON传入的对象创建内部类类型,但是虽然我已经阅读了这里以及Jackson的网站,但我似乎无法获得正确的组合,所以如果有其他人有任何指针,他们将非常感激。我在下面发布了一些片段并删除了所有的getter和setter,我没想到他们需要发帖。我正在使用Jackson 2.2。

I am trying to create the inner class type for an object being passed in as JSON but while I have read tons on here as well as Jackson's site I don't seem to be able to get the right combination so if anyone else has any pointers they would be much appreciated. I've posted some snippets below and removed all getters and setters, I didn't figure they needed posting. I'm using Jackson 2.2.

我正在尝试反序列化的类:

The classes I'm attempting to deserialize:

public class Settings {
  private int offset;
  private int limit;
  private String type;
  private Map<String, Criteria> criteria;

  public class Criteria {
    private String restriction;
    private Object value;
  }
}

我用来反序列化的代码:

The code I'm using to deserialize:

ObjectMapper om = new ObjectMapper();
TypeFactory tf = om.getTypeFactory();
JavaType map = tf.constructMapLikeType( Map.class, String.class, Criteria.class );
JavaType type = typeFactory.constructType( Settings.class, map );
Settings settings = om.readValue( entity, type );

我的JSON测试数据:

My JSON testing data:

{ "type": "org.json.Car", "criteria": { "restriction": "eq", "value": "bmw" } }


推荐答案

如果可以,那就让你的生活变得简单并将内部课程移到普通课堂使用Settings类中的引用。然后使用jackson进行编组,以下是如何使用类:

If you can, then make your life simple and move the inner class to a normal class with a reference in the Settings class. And then do the marshalling using jackson, here is how you can have your classes:

public class Settings {
  private int offset;
  private int limit;
  private String type;
  private Map<String, Criteria> criteria;
  private Criteria criteria;
}


 class Criteria {
    private String restriction;
    private Object value;
  }

这篇关于杰克逊杰森类型映射内部类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆