如何使用Jackson重命名JSON序列化中的根密钥
我正在使用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(); }
实施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序列化:
public class MySerializer { public static String serializeList(final List 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 list = new ArrayList(); 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.
我已经尝试了所有可以找到的建议解决方案,但未能实现我的目标。 我看过:
- jackson:自定义集合序列化到JSON
- 如何使用Java Jackson重命名JSON的根密钥?
- jackson:自定义集合序列化到JSON
或者我错过了什么? 我正在使用jackson1.9.12。 欢迎提供这方面的任何帮助。
好吧,默认情况下,Jackson在尝试确定要为包装值显示的根名称时使用两个注释之一 – @XmlRootElement
或@JsonRootName
。 它希望这个注释在被序列化的类型上,否则它将使用该类型的简单名称作为根名称。
在您的情况下,您正在序列化列表,这就是根名称为“ArrayList”(要序列化的类型的简单名称)的原因。 列表中的每个元素可以是使用@JsonRootName注释的类型,但列表本身不是 。
当您尝试换行的根值是一个集合时,您需要一些方法来定义换行名称:
持有人/包装类
您可以创建一个包装类来保存列表,并使用注释来定义所需的属性名称( 当您没有直接控制ObjectMapper
/ JSON转换过程时,您只需要使用此方法 ):
class MyInterfaceList { @JsonProperty("rootname") private List list; public List getList() { return list; } public void setList(List list) { this.list = list; } } final List lists = new ArrayList (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 lists = new ArrayList (4); lists.add(new MyImpl(1L, "test name")); final ObjectWriter writer = mapper.writer().withRootName("rootName"); final String json = writer.writeValueAsString(lists);
我知道,我迟到了,但我有更好的方法,不需要Holder / Wrapper Class。 它从注释中选择根密钥。
package com.test; import com.fasterxml.jackson.annotation.JsonRootName; @JsonRootName("Products") public class ProductDTO { private String name; private String description; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } }
这是测试类: –
package com.test; import java.io.IOException; import java.util.ArrayList; import org.junit.Test; import com.fasterxml.jackson.annotation.JsonRootName; import com.fasterxml.jackson.core.JsonGenerationException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; public class ProductDTOTestCase { @Test public void testPersistAndFindById() throws JsonGenerationException, JsonMappingException, IOException { ObjectMapper mapper = new ObjectMapper(); ProductDTO productDTO = new ProductDTO(); productDTO.setDescription("Product 4 - Test"); ArrayList arrayList = new ArrayList (); arrayList.add(productDTO); String rootName = ProductDTO.class.getAnnotation(JsonRootName.class).value(); System.out.println(mapper.writer().withRootName(rootName).writeValueAsString(arrayList)); } }
它将给出以下输出
{"Products":[{"name":null,"description":"Product 4 - Test"}]}
您需要在课程顶部使用此注释
@JsonTypeName("rootname")
@JsonTypeName("usuarios") @JsonTypeInfo(include= JsonTypeInfo.As.WRAPPER_OBJECT,use= JsonTypeInfo.Id.NAME) public class UsuarioDT extends ArrayList { @JsonProperty("rowsAffected") private Integer afectados; public Integer getAfectados() { return afectados; } public void setAfectados(Integer afectados) { this.afectados = afectados; } }
- 将ObjectNode写入JSON字符串,使用UTF-8字符转换为ASCII
- Spring 3使用Jackson将ArrayList序列化为JSON而不包装到对象(没有名称)
- 如何使用Java和Jackson Library对Json String进行多态反序列化?
- 如何使用Camel和Jackson库将嵌套的键,值对编组到JSON中?
- 单个自定义序列化程序,用于所有嵌入的带注释的对象,用它们的id替换它们
- RESTEASY003145:无法找到内容类型application / json的MessageBodyReader并键入class org.keycloak.representations.AccessTokenResponse
- 如何在Spring Boot 1.4中自定义Jackson
- 在Jackson中映射动态json对象字段?
- 尽管使用了JsonIgnore,但未能懒惰地初始化ManyToMany关系中的角色集合