Jackson转换为Collection、Array
转换 Array collection jackson
2023-09-11 14:19:38 时间
注意的地方就是实体类一定要有无参的构造方法,否则会报异常
//com.fasterxml.jackson.databind.exc.InvalidDefinitionException:
Cannot construct instance of `com.example.jackjson.UnmarshallCollectionOrArray$User` (no Creators, like default construct, exist):
cannot deserialize from Object value (no delegate- or property-based Creator)
1. Jackson转化为Array
package com.example.jackjson; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.type.CollectionType; import org.assertj.core.util.Lists; import org.junit.Assert; import org.junit.Test; import java.io.IOException; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; /** * @author: GuanBin * @date: Created in 下午2:33 2019/8/31 */ public class UnmarshallCollectionOrArray { @Test public void unmarshallToArray() throws IOException { ObjectMapper mapper = new ObjectMapper(); ArrayList<User> users = Lists.newArrayList(new User("tom", 10), new User("sam", 11)); String str = mapper.writeValueAsString(users); System.out.println("user json:" + str);
//若user没无参构造方法会报错 //com.fasterxml.jackson.databind.exc.InvalidDefinitionException:
Cannot construct instance of `com.example.jackjson.UnmarshallCollectionOrArray$User` (no Creators, like default construct, exist):
cannot deserialize from Object value (no delegate- or property-based Creator)
// at [Source: (String)"[{"name":"tom","age":10},{"name":"sam","age":11}]"; line: 1, column: 3] (through reference chain: java.lang.Object[][0]) User[] userArray = mapper.readValue(str, User[].class); Assert.assertTrue(userArray[0] instanceof User); } static class User { public User() { } public User(String name, int age) { this.name = name; this.age = age; } private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } }
2. Jackson转化为list
1)如果直接使用mapper.readValue(str, List.class); 虽然不会异常,但是list中的每个元素都是LinkedHashMap,而强转为User会报错;故如果转化为List<User> 用此方法是不行的
@Test public void unmarshallToList() throws IOException { ObjectMapper mapper = new ObjectMapper(); ArrayList<User> users = Lists.newArrayList(new User("tom", 10), new User("sam", 11)); String str = mapper.writeValueAsString(users); System.out.println("user json:" + str);
//若user没无参构造方法会报错 //com.fasterxml.jackson.databind.exc.InvalidDefinitionException:
Cannot construct instance of `com.example.jackjson.UnmarshallCollectionOrArray$User` (no Creators, like default construct, exist):
cannot deserialize from Object value (no delegate- or property-based Creator)
// at [Source: (String)"[{"name":"tom","age":10},{"name":"sam","age":11}]"; line: 1, column: 3] (through reference chain: java.lang.Object[][0])
List list = mapper.readValue(str, List.class); Assert.assertTrue(list.get(0) instanceof LinkedHashMap); }
2) 正确转化为list的有两种方式
1. 使用TypeReference
List<User> list = mapper.readValue(str, new TypeReference<List<User>>() { });
@Test public void unmarshallToListOneWay() throws IOException { ObjectMapper mapper = new ObjectMapper(); ArrayList<User> users = Lists.newArrayList(new User("tom", 10), new User("sam", 11)); String str = mapper.writeValueAsString(users); System.out.println("user json:" + str);
//若user没无参构造方法会报错 //com.fasterxml.jackson.databind.exc.InvalidDefinitionException:
Cannot construct instance of `com.example.jackjson.UnmarshallCollectionOrArray$User` (no Creators, like default construct, exist):
cannot deserialize from Object value (no delegate- or property-based Creator)
// at [Source: (String)"[{"name":"tom","age":10},{"name":"sam","age":11}]"; line: 1, column: 3] (through reference chain: java.lang.Object[][0])
List<User> list = mapper.readValue( str, new TypeReference<List<User>>() { }); Assert.assertTrue(list.get(0) instanceof User); }
2. 获取CollectionType
CollectionType javaType = mapper.getTypeFactory().constructCollectionType(List.class, User.class);
List<User> list = mapper.readValue(str, javaType);
@Test public void unmarshallToListTwoWay() throws IOException { ObjectMapper mapper = new ObjectMapper(); ArrayList<User> users = Lists.newArrayList(new User("tom", 10), new User("sam", 11)); String str = mapper.writeValueAsString(users); System.out.println("user json:" + str);
//若user没无参构造方法会报错 //com.fasterxml.jackson.databind.exc.InvalidDefinitionException:
Cannot construct instance of `com.example.jackjson.UnmarshallCollectionOrArray$User` (no Creators, like default construct, exist):
cannot deserialize from Object value (no delegate- or property-based Creator)
// at [Source: (String)"[{"name":"tom","age":10},{"name":"sam","age":11}]"; line: 1, column: 3] (through reference chain: java.lang.Object[][0]) CollectionType javaType = mapper.getTypeFactory().constructCollectionType(List.class, User.class); List<User> list = mapper.readValue(str, javaType); Assert.assertTrue(list.get(0) instanceof User); }
转 : https://www.cnblogs.com/guanbin-529/p/11441805.html
相关文章
- C#中使用Buffer.BlockCopy()方法将string转换为byte array的方法:
- What’s the difference between “Array()” and “[]” while declaring a JavaScript array?
- Why does typeof array with objects return “Object” and not “Array”?
- 矢量图形转换工具UniConvertor
- python数字图像处理(4):图像数据类型及颜色空间转换
- SwiftUI基础之JSON和Array之间互相转换(教程含源码)
- 既然红黑树那么好,为啥hashmap不直接采用红黑树,而是当大于8个的时候才转换红黑树?
- SQL Server日期与字符串之间的转换
- CAD怎么快速转换图纸中的图层标准?CAD图层转换技巧