如何使用 Jackson 在 JSON 序列化中重命名根密钥 [英] How to rename root key in JSON serialization with Jackson

查看:22
本文介绍了如何使用 Jackson 在 JSON 序列化中重命名根密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Jackson 对对象列表进行 JSON 序列化.

I am using Jackson for JSON serialization of a list of objects.

这是我得到的:

{"ArrayList":[{"id":1,"name":"test name"}]}

但我想要这个:

{"rootname":[{"id":1,"name":"test name"}]} // ie showing the string I want as the root name.

以下是我的方法:

界面:

public interface MyInterface {
    public long getId();
    public String getName();
}

实现类:

@JsonRootName(value = "rootname")
public class MyImpl implements MyInterface {
    private final long id;
    private String name;

    public MyImpl(final long id,final name) {
        this.id = id;
        this.name = name;
    }

   // getters     
}

JSon 序列化:

public class MySerializer {
    public static String serializeList(final List<MyInterface> lists) {
        //check for null value.Throw Exception
        final ObjectMapper mapper = new ObjectMapper();
        mapper.configure(SerializationConfig.Feature.WRAP_ROOT_VALUE, true);
        return mapper.writeValueAsString(lists);
    }
}

测试:

final List<MyInterface> list = new ArrayList<MyImpl>();
MyImpl item = new MyImpl(1L,"test name");
list.add(item);
final String json = MySerializer.serializeList(list);
System.out.println(json);

这是我得到的:

{"ArrayList":[{"id":1,"name":"test name"}]}

但我想要这个:

{"rootname":[{"id":1,"name":"test name"}]} // ie showing the string I want as the root     name.

我已经尝试了我能找到的所有建议的解决方案,但未能实现我的目标.我看过:

I have tried all suggested solutions I could find but failed to achieve my goal. I have looked at:

还是我遗漏了什么?我为此使用 jackson 1.9.12.欢迎在这方面提供任何帮助.

Or am I missing something? I am using jackson 1.9.12 for this. Any help in this regard is welcome.

推荐答案

嗯,默认情况下,Jackson 在尝试确定要为包装值显示的根名称时使用两个注释之一 - @XmlRootElement@JsonRootName.它期望这个注释在被序列化的类型上,否则它将使用 简单名称 作为根名称的类型.

Well, by default Jackson uses one of two annotations when trying to determine the root name to be displayed for wrapped values - @XmlRootElement or @JsonRootName. It expects this annotation to be on the type being serialized, else it will use the simple name of the type as the root name.

在您的情况下,您正在序列化一个列表,这就是根名称是ArrayList"(被序列化类型的简单名称)的原因.列表中的每个元素都可能是用@JsonRootName 注释的类型,但列表本身不是.

In your case, you are serializing a list, which is why the root name is 'ArrayList' (simple name of the type being serialized). Each element in the list may be of a type annotated with @JsonRootName, but the list itself is not.

当您尝试包装的根值是一个集合时,您需要某种方式来定义包装名称:

When the root value you are trying to wrap is a collection then you need some way of defining the wrap name:

您可以创建一个包装类来保存列表,并使用注释来定义所需的属性名称(只有当您没有直接控制 ObjectMapper/JSON 转换过程):

You can create a wrapper class to hold the list, with an annotation to define the desired property name (you only need to use this method when you do not have direct control of the ObjectMapper/JSON transformation process):

class MyInterfaceList {
    @JsonProperty("rootname")
    private List<MyInterface> list;

    public List<MyInterface> getList() {
        return list;
    }

    public void setList(List<MyInterface> list) {
        this.list = list;
    }
}

final List<MyInterface> lists = new ArrayList<MyInterface>(4);
lists.add(new MyImpl(1L, "test name"));
MyInterfaceList listHolder = new MyInterfaceList();
listHolder.setList(lists);
final String json = mapper.writeValueAsString(listHolder);

对象编写器

这是最好的选择.使用配置的 ObjectWriter 实例来生成 JSON.特别是,我们对 withRootName 方法:

final List<MyInterface> lists = new ArrayList<MyInterface>(4);
lists.add(new MyImpl(1L, "test name"));
final ObjectWriter writer = mapper.writer().withRootName("rootName");
final String json = writer.writeValueAsString(lists);

这篇关于如何使用 Jackson 在 JSON 序列化中重命名根密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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