1. Jackson转化为Array
注意的地方就是实体类一定要有无参的构造方法,否则会报异常
//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)
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)