JSON字符串转List对象

1. 引入jar包

<!-- https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-mapper-asl -->
<dependency>
     <groupId>org.codehaus.jackson</groupId>
     <artifactId>jackson-mapper-asl</artifactId>
     <version>1.9.13</version>
</dependency>

2. 具体代码

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

import java.io.IOException;
import java.util.List;

/**
 * JSON String转List对象
 * @author zhongpengfei
 */
public class ObjectChange {

    public static void main(String[] args) throws IOException {
        String result = "[
" +
                "    {
" +
                "    "name":"wangwu",
" +
                "    "level":8,
" +
                "    "sex":1
" +
                "    },
" +
                "    {
" +
                "    "name":"zhangsan",
" +
                "    "level":2,
" +
                "    "sex":1
" +
                "    },
" +
                "    {
" +
                "    "name":"lisi",
" +
                "    "level":4,
" +
                "    "sex":0
" +
                "    }
" +
                "]";
        ObjectMapper mapper = new ObjectMapper();
        List<Person> list = mapper.readValue(result,new TypeReference<List<Person>>(){});
        for (Person person : list) {
            System.out.println(person);
        }
    }
}

3. 结果

原文地址:https://www.cnblogs.com/PersonalDiary/p/13808983.html