java - Jackson Annotation 创建本地 JSON 引用

标签 java json jackson jersey-2.0

我有以下类结构

public class A {
    @Id
    public String id;


    public List<B> bs;
}

public class B{
    public String name;

    public List<B> preconditions;

}

如果我返回这样一个类的实例,那么我会得到一个嵌套的 JSON 结构

{
    "id": "3",
    "bs": [
        {
            "name": "abc"
        },
        {
            "name": "drilling",
            "preconditions": [
                {
                    "name": "abc"
                }
            ]
        }
    ]
}

我希望先决条件成为引用文献列表,例如

{
    "id": "3",
    "bs": [
        {
            "name": "abc"
        },
        {
            "name": "drilling",
            "preconditions": 
                    ["abc"]
        }
    ]
}

我怎样才能做到这一点?前提条件应引用 bs 列表中的对象,我使用 Jersey Web SerevicesJackson 进行 JSON 生成。

编辑:基于 Michał Ziober 的回答: 它工作正常,但如果我更改 bs 的顺序,结果如下所示:

{
    "id": "3",
    "bs": [
        {
            "name": "drilling",
            "preconditions": 
                    [
                       {
                            "name": "abc"
                       }
                    ]
        },"abc"
    ]
}

我希望对象始终在 bs 列表中定义,而不是在前置条件列表中

{
    "id": "3",
    "bs": [
        {
            "name": "drilling",
            "preconditions": 
                    ["abc"]
        }, 
        {
            "name": "abc"
        }
    ]
}

最佳答案

您需要使用JsonIdentityInfo注释。您的 POJO 类可能如下所示:

class A {

    private String id;

    @JsonIdentityInfo(property = "name", generator = ObjectIdGenerators.PropertyGenerator.class)
    private List<B> bs;

    // getters, setters
}

class B {

    private String name;

    @JsonIdentityInfo(property = "name", generator = ObjectIdGenerators.PropertyGenerator.class)
    private List<B> preconditions;

    // getters, setters
}

简单的例子:

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.util.Arrays;
import java.util.List;

public class JsonApp {

    public static void main(String[] args) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        mapper.enable(SerializationFeature.INDENT_OUTPUT);
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

        B b = new B();
        b.setName("abc");

        B b1 = new B();
        b1.setName("drilling");
        b1.setPreconditions(Arrays.asList(b));

        A a = new A();
        a.setId("3");
        a.setBs(Arrays.asList(b, b1));

        System.out.println(mapper.writeValueAsString(a));
    }
}

打印:

{
  "id" : "3",
  "bs" : [ {
    "name" : "abc"
  }, {
    "name" : "drilling",
    "preconditions" : [ "abc" ]
  } ]
}

关于java - Jackson Annotation 创建本地 JSON 引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55143820/

相关文章:

json - 如何将 node.js sqlite3 记录集格式化为记录数组的 JSON 对象

spring - Jackson 将日期序列化为时间戳

java - Camel 测试 :Caused by: java. lang.ClassNotFoundException : com. ibm.ws.bootstrap.RASWsLoggerFactory

java - 如何将 CSS 用于 Vaadin 组件?

javascript - 在不重新加载页面的情况下更新页面数据的正确过程

php - 我无法将 Json 转换为字符串 [OctoberCms]

java - Android:如何将按钮文本中的数字转换为整数?

java - Android Studio-未找到JVM安装

json - JSONObject 的 Jackson 2 等价物是什么?

java - Avro 使用 json 转换生成类问题 [kotlin]