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

以下是我得到的:

{"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();
}

Implementation class:

@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 serialization:

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.

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

还是我错过了什么?为此,我使用的是Jackson 1.9.12.欢迎在这方面提供任何帮助.

推荐答案

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.

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:

Holder/Wrapper类

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);

对象编写器

这是更可取的 Select .使用配置的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);

Json相关问答推荐

使用Powershell脚本将配置信息块添加到json文件

使用Jolt变换转换JsonArray以将关键字转移到内部JsonArray中

SWIFT中的网络经理

织女星-没有循环的动画条形图第二部分(实际上是织女星)

交换键和数组值,将旧键转换为新数组值,使用 jq

使用 Powershell,如何将 Azure AD 组成员转换为 Json 对象(文件),然后可以更新?

展平多个数组以保持顺序

如何按键过滤

从 JSON 响应中获取最新版本发布字段

坚持弄清楚如何使用 api 响应来调用以从不同的链接检索响应

C# 合并 2 个几乎相同的 JSON 对象

在 JSON 字符串中react i18n 换行符

以 unicode 将 pandas DataFrame 写入 JSON

在 Http Header 中使用 Json 字符串

JSON.NET JsonConvert 与 .NET JavaScriptSerializer

gson:将 null 视为空字符串

将序列化表单的数据转换为 json 对象

Django:TypeError:[] 不是 JSON 可序列化的为什么?

如何使用 Gson 解码具有未知字段的 JSON?

是否有一个 PHP 函数只在双引号而不是单引号中添加斜杠