fastJSON使用(二)——json转换成对象

来源:互联网 发布:java object to date 编辑:程序博客网 时间:2024/06/09 16:17

fastJSON使用(二)——json转换成对象

下面介绍一下利用fastJSON把json串转换成对象
首先需要引入fastJSON的bao
<dependency>            <groupId>com.alibaba</groupId>            <artifactId>fastjson</artifactId>            <version>1.2.17</version>        </dependency>

对于常用的类型进行转换:
1、普通map
2、List<Map<String,String>>
3、Map<String,Object>
4、List<Student>

package fastjsonstudy;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.TypeReference;import java.util.*;/** * Hello world! */public class App {    public static void main(String[] args) {        //普通map        String jsonString1 = "{\"param5\":\"value5\",\"param3\":\"value3\",\"param4\":\"value4\",\"param1\":\"value1\",\"param2\":\"value2\"}";        System.out.println(jsonString1);        Map<String,String> stringStringMap = (Map<String,String>)JSON.parse(jsonString1);        for (String s : stringStringMap.keySet()) {            System.out.println(s + "==>" +stringStringMap.get(s));        }        System.out.println("===================================================");        //List<Map<String,String>>        String jsonString2 = "[{\"param5\":\"value5\",\"param3\":\"value3\",\"param4\":\"value4\",\"param1\":\"value1\",\"param2\":\"value2\"},{\"p1\":\"v1\",\"p2\":\"v2\",\"p3\":\"v3\",\"p4\":\"v4\",\"p5\":\"v5\"}]";        System.out.println(jsonString2);        List<Map<String,String>> mapList = JSON.parseObject(jsonString2, new TypeReference<List<Map<String,String>>>(){});        for (Map<String, String> stringObjectMap : mapList) {            for (String s : stringObjectMap.keySet()) {                System.out.println(s + "==>" + stringObjectMap.get(s));            }        }        System.out.println("===================================================");        //Map<String,Object> ==> Object还能够进行分解        String jsonString3 = "{\"count\":2,\"list\":[{\"param5\":\"value5\",\"param3\":\"value3\",\"param4\":\"value4\",\"param1\":\"value1\",\"param2\":\"value2\"},{\"p1\":\"v1\",\"p2\":\"v2\",\"p3\":\"v3\",\"p4\":\"v4\",\"p5\":\"v5\"}]}";        System.out.println(jsonString3);        Map<String,Object> map = JSON.parseObject(jsonString3);        System.out.println(map.get("count"));        String tempjsonString3 = map.get("list").toString();        System.out.println(tempjsonString3);        List<Map<String,String>> mapList2 = JSON.parseObject(tempjsonString3, new TypeReference<List<Map<String,String>>>(){});        for (Map<String, String> stringObjectMap : mapList2) {            for (String s : stringObjectMap.keySet()) {                System.out.println(s + "==>" + stringObjectMap.get(s));            }        }        System.out.println("===================================================");        //解析已有的对象        String jsonString4 = "[{\"age\":12,\"date\":1465475917155,\"name\":\"s1\"},{\"age\":12,\"date\":1465475917175,\"name\":\"s2\"},{\"age\":12,\"date\":1465475917175,\"name\":\"s3\"},{\"age\":12,\"date\":1465475917175,\"name\":\"s4\"},{\"age\":12,\"date\":1465475917175,\"name\":\"s5\"},{\"age\":12,\"date\":1465475917175,\"name\":\"s6\"}]";        System.out.println(jsonString4);        List<Student> studentList = JSON.parseArray(jsonString4,Student.class);        for (Student student : studentList) {            System.out.println(student.getName());        }        System.out.println("===================================================");        //解析已有的对象的另一种方式        System.out.println(jsonString4);        List<Student> studentList2 = JSON.parseObject(jsonString4,new TypeReference<List<Student>>(){});        for (Student student : studentList2) {            System.out.println(student.getName());        }    }}

package fastjsonstudy;import java.util.Date;/** * Created by Administrator on 2016-10-21. */public class Student {    private String name;    private int age;    private Date date;    public Student() {        // TODO Auto-generated constructor stub    }    public Student(String name,int age,Date date){        this.name = name;        this.age = age;        this.date = date;    }    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;    }    public Date getDate() {        return date;    }    public void setDate(Date date) {        this.date = date;    }}

下面是运行结果:
{"param5":"value5","param3":"value3","param4":"value4","param1":"value1","param2":"value2"}param5==>value5param3==>value3param4==>value4param1==>value1param2==>value2===================================================[{"param5":"value5","param3":"value3","param4":"value4","param1":"value1","param2":"value2"},{"p1":"v1","p2":"v2","p3":"v3","p4":"v4","p5":"v5"}]param5==>value5param3==>value3param4==>value4param1==>value1param2==>value2p1==>v1p2==>v2p3==>v3p4==>v4p5==>v5==================================================={"count":2,"list":[{"param5":"value5","param3":"value3","param4":"value4","param1":"value1","param2":"value2"},{"p1":"v1","p2":"v2","p3":"v3","p4":"v4","p5":"v5"}]}2[{"param5":"value5","param3":"value3","param4":"value4","param1":"value1","param2":"value2"},{"p1":"v1","p2":"v2","p3":"v3","p4":"v4","p5":"v5"}]param5==>value5param3==>value3param4==>value4param1==>value1param2==>value2p1==>v1p2==>v2p3==>v3p4==>v4p5==>v5===================================================[{"age":12,"date":1465475917155,"name":"s1"},{"age":12,"date":1465475917175,"name":"s2"},{"age":12,"date":1465475917175,"name":"s3"},{"age":12,"date":1465475917175,"name":"s4"},{"age":12,"date":1465475917175,"name":"s5"},{"age":12,"date":1465475917175,"name":"s6"}]s1s2s3s4s5s6===================================================[{"age":12,"date":1465475917155,"name":"s1"},{"age":12,"date":1465475917175,"name":"s2"},{"age":12,"date":1465475917175,"name":"s3"},{"age":12,"date":1465475917175,"name":"s4"},{"age":12,"date":1465475917175,"name":"s5"},{"age":12,"date":1465475917175,"name":"s6"}]s1s2s3s4s5s6


0 0
原创粉丝点击