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 = "[\n" +
                "    {\n" +
                "    \"name\":\"wangwu\",\n" +
                "    \"level\":8,\n" +
                "    \"sex\":1\n" +
                "    },\n" +
                "    {\n" +
                "    \"name\":\"zhangsan\",\n" +
                "    \"level\":2,\n" +
                "    \"sex\":1\n" +
                "    },\n" +
                "    {\n" +
                "    \"name\":\"lisi\",\n" +
                "    \"level\":4,\n" +
                "    \"sex\":0\n" +
                "    }\n" +
                "]";
        ObjectMapper mapper = new ObjectMapper();
        List<Person> list = mapper.readValue(result,new TypeReference<List<Person>>(){});
        for (Person person : list) {
            System.out.println(person);
        }
    }
}

3. 结果

 

posted @ 2020-10-13 15:12  减字木兰花·回风落景  阅读(1041)  评论(0编辑  收藏  举报