在JSON中创建父属性 [英] Creating Parent Property in JSON

查看:139
本文介绍了在JSON中创建父属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从同一对象创建JSON的父级和子级属性,这意味着它应该类似于

I have requirement to create a parent and child property of JSON from same object which means it should be like

 {"employee": {
   "name":"skanda", 
   "id":"123", 
   "employee":[
      {"id":"345"}
      ]
     }
  }

这是我的Java对象.请让我知道我该怎么做.我是否应该创建内部类并具有一个以上Employee对象的实例.请指教.我必须以这样的方式进行设计:当通过Rest WS传递时,也应在另一端对它进行反序列化.如果我可以使用注释,请告知在这种情况下应使用哪些注释.

Here is my Java Object. Please let me know how can I do this. Should I create Inner classes and have instance of one more Employee object. Please advise. I have to design in such a way that the same should also be De-serialized on other end when passed through Rest WS. If I can use annotations please advise which annotations should be used in this case.

public class Employee {
  private String name;
  private String id;
  private List<Employee> list = new ArrayList<Employee>();

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

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

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

}

推荐答案

您可以使用Jackson和注释.您将需要"jackson-databind".

You can use Jackson and annotations. You will need "jackson-databind".

在Maven上,添加如下所示的依赖项:

On maven, add the dependency like this:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.4.4</version>
</dependency>

使用这样的POJO:

import java.util.List;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonRootName;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;

@JsonRootName(value = "employee")
@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@id")
public class Employee {

    private String name;

    private String id;

    private List<Employee> employee;

    public String getName() {
        return name;
    }

    public void setName(final String name) {
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(final String id) {
        this.id = id;
    }

    public List<Employee> getEmployee() {
        return employee;
    }

    public void setEmployees(final List<Employee> employee) {
        this.employee = employee;
    }

}

还有这样的例子:

import java.util.ArrayList;
import java.util.List;

import org.junit.Assert;
import org.junit.Test;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

public class EmployeeTest {

    private static final String EXPECTED_JSON_RESULT = "{\"employee\":{\"@id\":1,\"name\":\"skanda\",\"id\":\"123\",\"employee\":[{\"@id\":2,\"id\":\"345\"}]}}";

    @Test
    public void serializationTest() throws JsonProcessingException {
        final Employee emp1 = new Employee();
        emp1.setId("123");
        emp1.setName("skanda");

        final Employee empL1 = new Employee();
        empL1.setId("345");

        final List<Employee> list = new ArrayList<>();
        list.add(empL1);

        emp1.setEmployees(list);

        final ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setSerializationInclusion(Include.NON_NULL);
        objectMapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);

        Assert.assertEquals(EXPECTED_JSON_RESULT, objectMapper.writeValueAsString(emp1));
    }

}

将生成这样的JSON:

Will generate a JSON like this:

{
    "employee":{
        "@id":1,
        "name":"skanda",
        "id":"123",
        "employee":[
            {
                "@id":2,
                "id":"345"
            }
        ]
    }
}

@JsonIdentityInfo将帮助您进行自我参考,避免使用StackOverflowError,并且已针对@id属性进行了映射.您可以根据需要删除.

@JsonIdentityInfo will help you on self-references, avoiding StackOverflowError and is mapped for @idproperty. You can remove, if you want.

有关完整文档,请参见 https://github.com/FasterXML/jackson

For complete documentation, see https://github.com/FasterXML/jackson

这篇关于在JSON中创建父属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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