在 JSON 中创建父属性

Creating Parent Property in JSON

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

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

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

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 JSON 库实现此目的:http://jackson.codehaus.org/

这里有一个特别针对嵌套对象的好教程:http://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/

除此之外,Google是你的朋友:)

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

在maven上,像这样添加依赖:

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

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

@JsonIdentityInfo 将帮助您进行自我引用,避免 WhosebugError 并映射到 @id属性。如果需要,您可以删除。

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