fastjson json字符串和JavaBean、List、Map及复杂集合类型的相互转换

【转】https://blog.csdn.net/silencecarrot/article/details/79381189

本文主要示例两部分内容:

  • JavaBean、List、Map、复杂集合 转换成 json字符串;
  • json字符串 转换成 JavaBean、List、Map、复杂集合;

定义POJO:

public class A {

    private String usename;
    private String password;

    public A() {
    }

    public A(String usename, String password) {
        this.usename = usename;
        this.password = password;
    }

    public String getUsename() {
        return usename;
    }

    public void setUsename(String usename) {
        this.usename = usename;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

开始转换测试:

public static void main(String[] args) {

        //javaBean 转 json字符串
        A a1 = new A("wei.hu", "123456");

        String a1Json = JSON.toJSONString(a1);
        System.out.println(a1Json);



        //List 转 json字符串
        A a2 = new A("mengna.shi", "123456");
        A a3 = new A("ming.li", "567890");

        List aList = Lists.newArrayList(a1, a2, a3);
        String aListJson = JSON.toJSONString(aList);
        System.out.println(aListJson);

        //List 转 json字符串
        List stringList = Lists.newArrayList("wei.hu", "mengna.shi", "fastJson");

        String stringListJson = JSON.toJSONString(stringList);
        System.out.println(stringListJson);

        //List 转 json字符串
        List integerList = Lists.newArrayList(10, 9, 8, 7);

        String integerListJson = JSON.toJSONString(integerList);
        System.out.println(integerListJson);



        //Map 转 json字符串
        Map aMap = Maps.newHashMap();
        aMap.put("a1", a1);
        aMap.put("a2", a2);
        aMap.put("a3", a3);

        String aMapJson = JSON.toJSONString(aMap);
        System.out.println(aMapJson);

        //Map 转 json字符串
        Map stringMap = Maps.newHashMap();
        stringMap.put("name", "wei.hu");
        stringMap.put("gender", "man");
        stringMap.put("age", "18");

        String stringMapJson = JSON.toJSONString(stringMap);
        System.out.println(stringMapJson);

        //Map 转 json字符串
        Map integerMap = Maps.newHashMap();
        integerMap.put("int1",18);
        integerMap.put("int2", 19);
        integerMap.put("int3", 20);

        String integerMapJson = JSON.toJSONString(integerMap);
        System.out.println(integerMapJson);

        //Map 转 json字符串
        Map objectMap = Maps.newHashMap();
        objectMap.put("name", "wei.hu");
        objectMap.put("gender", "man");
        objectMap.put("age", 18);

        String objectMapJson = JSON.toJSONString(objectMap);
        System.out.println(objectMapJson);



        //List> 转 json字符串
        Map aMap1 = Maps.newHashMap();
        aMap1.put("a1", a1);
        aMap1.put("a2", a2);

        List> aList1 = Lists.newArrayList();
        aList1.add(aMap);
        aList1.add(aMap1);

        String complexJson1 = JSON.toJSONString(aList1);
        System.out.println(complexJson1);

        //Map> 转 json字符串
        List aList2 = Lists.newArrayList(a1, a2);
        List aList3 = Lists.newArrayList(a2, a3);

        Map> listMap = Maps.newHashMap();
        listMap.put("key1", aList2);
        listMap.put("key2", aList3);

        String complexJson2 = JSON.toJSONString(listMap);
        System.out.println(complexJson2);



        //json字符串 转 javaBean
        String jsonString1 = "{\"password\":\"123456\",\"usename\":\"wei.hu\"}";

        A aa1 = JSON.parseObject(jsonString1, A.class);
        System.out.println(aa1.getUsename() + " / " + aa1.getPassword());


        //json字符串 转 List
        String jsonString2 = "[{\"password\":\"123456\",\"usename\":\"wei.hu\"},{\"password\":\"123456\",\"usename\":\"mengna.shi\"},{\"password\":\"567890\",\"usename\":\"ming.li\"}]";

        List aList4 = JSON.parseArray(jsonString2, A.class);
        System.out.println(aList4.size());


        //json字符串 转 List
        String jsonString3 = "[\"wei.hu\",\"mengna.shi\",\"fastJson\"]";

        List stringList1 = JSON.parseArray(jsonString3, String.class);
        System.out.println(stringList1.size());


        //json字符串 转 List
        String jsonString4 = "[10,9,8,7]";

        List integerList1 = JSON.parseArray(jsonString4, Integer.class);
        System.out.println(integerList1.size());


        //json字符串转 Map
        String jsonString5 = "{\"a1\":{\"password\":\"123456\",\"usename\":\"wei.hu\"},\"a2\":{\"password\":\"123456\",\"usename\":\"mengna.shi\"},\"a3\":{\"password\":\"567890\",\"usename\":\"ming.li\"}}";

        Map stringObjectMap = JSON.parseObject(jsonString5, Map.class);
        System.out.println(stringObjectMap.size());



        //json字符串 转 List>
        String jsonString6 = "[{\"a1\":{\"password\":\"123456\",\"usename\":\"wei.hu\"},\"a2\":{\"password\":\"123456\",\"usename\":\"mengna.shi\"},\"a3\":{\"password\":\"567890\",\"usename\":\"ming.li\"}},{\"a1\":{\"$ref\":\"$[0].a1\"},\"a2\":{\"$ref\":\"$[0].a2\"}}]";

        List> mapList = JSON.parseObject(jsonString6, new TypeReference>>(){});
        System.out.println("mapList.size():" + mapList.size());

        //json字符串 转 Map>
        String jsonString7 = "{\"key1\":[{\"password\":\"123456\",\"usename\":\"wei.hu\"},{\"password\":\"123456\",\"usename\":\"mengna.shi\"}],\"key2\":[{\"$ref\":\"$.key1[1]\"},{\"password\":\"567890\",\"usename\":\"ming.li\"}]}";

        Map> listMap1 = JSON.parseObject(jsonString7, new TypeReference>>() {});
        System.out.println("listMap1.size():" + listMap1.size());


        //json字符串 转 List>>
        A aaa1 = new A("wei.hu", "123456");
        A aaa2 = new A("mengna.shi", "abcdef");
        A aaa3 = new A("admin", "098765");
        List myAList1 = Lists.newArrayList(aaa1, aaa2, aaa3);

        A aaa4 = new A("song.xu", "28");
        A aaa5 = new A("jielun.zhou", "36");
        List myAList2 = Lists.newArrayList(aaa4, aaa5);

        Map> myMap1 = Maps.newHashMap();
        myMap1.put("myAList1", myAList1);
        myMap1.put("myAList2", myAList2);


        A aaa6 = new A("junjie.lin", "61");
        A aaa7 = new A("jian.xiao", "31");
        A aaa8 = new A("xi.ben", "32");
        List myAList3 = Lists.newArrayList(aaa6, aaa7, aaa8);

        Map> myMap2 = Maps.newHashMap();
        myMap2.put("myAList3", myAList3);


        A aaa9 = new A("xing.qun", "33");
        A aaa10 = new A("datong.fang", "34");
        A aaa11 = new A("dun.tong", "35");
        List myAList4 = Lists.newArrayList(aaa9, aaa10, aaa11);

        Map> myMap3 = Maps.newHashMap();
        myMap3.put("myAList4", myAList4);

        List>> list = Lists.newArrayList(myMap1, myMap2, myMap3);

        System.out.println(JSON.toJSONString(list));

        List>> newList = JSON.parseObject(JSON.toJSONString(list), new TypeReference>>>() {});
        System.out.println("newList.size():" + newList.size());


        //josn字符串 转 Map>>
        A objectA1 = new A("1", "1");
        A objectA2 = new A("2", "2");
        A objectA3 = new A("3", "3");

        Map newMap1 = Maps.newHashMap();
        newMap1.put("objectA1", objectA1);
        newMap1.put("objectA2", objectA2);
        newMap1.put("objectA3", objectA3);


        A objectA4 = new A("4", "4");
        A objectA5 = new A("5", "5");
        A objectA6 = new A("6", "6");

        Map newMap2 = Maps.newHashMap();
        newMap2.put("objectA4", objectA4);
        newMap2.put("objectA5", objectA5);
        newMap2.put("objectA6", objectA6);

        List> newList1 = Lists.newArrayList(newMap1, newMap2);


        A objectA7 = new A("7", "7");
        A objectA8 = new A("8", "8");

        Map newMap3 = Maps.newHashMap();
        newMap3.put("objectA7", objectA7);
        newMap3.put("objectA8", objectA8);

        List> newList2 = Lists.newArrayList(newMap3);


        A objectA9 = new A("9", "9");
        A objectA0 = new A("0", "0");

        Map newMap4 = Maps.newHashMap();
        newMap4.put("objectA9", objectA9);
        newMap4.put("objectA0", objectA0);

        List> newList3 = Lists.newArrayList(newMap4);

        Map>> map = Maps.newHashMap();
        map.put("newList1", newList1);
        map.put("newList2", newList2);
        map.put("newList3", newList3);

        System.out.println(JSON.toJSONString(map));


        Map>> newMap = JSON.parseObject(JSON.toJSONString(map), new TypeReference>>>() {});
        System.out.println("newMap.size()" + newMap.size());
    }

输出:

{"password":"123456","usename":"wei.hu"}
[{"password":"123456","usename":"wei.hu"},{"password":"123456","usename":"mengna.shi"},{"password":"567890","usename":"ming.li"}]
["wei.hu","mengna.shi","fastJson"]
[10,9,8,7]
{"a1":{"password":"123456","usename":"wei.hu"},"a2":{"password":"123456","usename":"mengna.shi"},"a3":{"password":"567890","usename":"ming.li"}}
{"gender":"man","name":"wei.hu","age":"18"}
{"int2":19,"int1":18,"int3":20}
{"gender":"man","name":"wei.hu","age":18}
[{"a1":{"password":"123456","usename":"wei.hu"},"a2":{"password":"123456","usename":"mengna.shi"},"a3":{"password":"567890","usename":"ming.li"}},{"a1":{"$ref":"$[0].a1"},"a2":{"$ref":"$[0].a2"}}]
{"key1":[{"password":"123456","usename":"wei.hu"},{"password":"123456","usename":"mengna.shi"}],"key2":[{"$ref":"$.key1[1]"},{"password":"567890","usename":"ming.li"}]}
wei.hu / 123456
3
3
4
3
mapList.size():2
listMap1.size():2
[{"myAList2":[{"password":"28","usename":"song.xu"},{"password":"36","usename":"jielun.zhou"}],"myAList1":[{"password":"123456","usename":"wei.hu"},{"password":"abcdef","usename":"mengna.shi"},{"password":"098765","usename":"admin"}]},{"myAList3":[{"password":"61","usename":"junjie.lin"},{"password":"31","usename":"jian.xiao"},{"password":"32","usename":"xi.ben"}]},{"myAList4":[{"password":"33","usename":"xing.qun"},{"password":"34","usename":"datong.fang"},{"password":"35","usename":"dun.tong"}]}]
newList.size():3
{"newList3":[{"objectA9":{"password":"9","usename":"9"},"objectA0":{"password":"0","usename":"0"}}],"newList2":[{"objectA7":{"password":"7","usename":"7"},"objectA8":{"password":"8","usename":"8"}}],"newList1":[{"objectA2":{"password":"2","usename":"2"},"objectA3":{"password":"3","usename":"3"},"objectA1":{"password":"1","usename":"1"}},{"objectA6":{"password":"6","usename":"6"},"objectA4":{"password":"4","usename":"4"},"objectA5":{"password":"5","usename":"5"}}]}
newMap.size()3

你可能感兴趣的:(fastjson json字符串和JavaBean、List、Map及复杂集合类型的相互转换)