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

前面一篇《fastjson json字符串和JavaBean、List、Map及复杂集合类型的相互转换》我们讨论了json和javabean之间的转换,在实际应用中发现,我们举得例子还不够复杂。
如果说javabean中嵌套了其他javabean,并且嵌套了复杂List、Map等的情况时,fastjson是否还能很好的支持这个转换呢?答案是肯定的,fastjson完全支持。
A.java

import com.google.common.collect.Lists;

import java.util.List;
import java.util.Map;

public class A {
    String name;
    String gender;
    List<String> hobby;
    List<Map<String, String>> xxx;
    B b;
    Map<C, List<Map<Integer, C>>> bListMap;

    public A() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public List<String> getHobby() {
        return hobby;
    }

    public void setHobby(List<String> hobby) {
        this.hobby = hobby;
    }

    public List<Map<String, String>> getXxx() {
        return xxx;
    }

    public void setXxx(List<Map<String, String>> xxx) {
        this.xxx = xxx;
    }

    public B getB() {
        return b;
    }

    public void setB(B b) {
        this.b = b;
    }

    public Map<C, List<Map<Integer, C>>> getbListMap() {
        return bListMap;
    }

    public void setbListMap(Map<C, List<Map<Integer, C>>> bListMap) {
        this.bListMap = bListMap;
    }
}

B.java

import java.util.List;
import java.util.Map;

public class B {
    String sa;
    int sb;
    List integerList;
    Map> stringListMap;

    public B() {
    }

    public String getSa() {
        return sa;
    }

    public void setSa(String sa) {
        this.sa = sa;
    }

    public int getSb() {
        return sb;
    }

    public void setSb(int sb) {
        this.sb = sb;
    }

    public List getIntegerList() {
        return integerList;
    }

    public void setIntegerList(List integerList) {
        this.integerList = integerList;
    }

    public Map> getStringListMap() {
        return stringListMap;
    }

    public void setStringListMap(Map> stringListMap) {
        this.stringListMap = stringListMap;
    }
}

C.java

public class C {
    String aaa;
    int bbb;
    Float ccc;

    public C() {
    }

    public C(String aaa, int bbb, float ccc) {
        this.aaa = aaa;
        this.bbb = bbb;
        this.ccc = ccc;
    }

    public String getAaa() {
        return aaa;
    }

    public void setAaa(String aaa) {
        this.aaa = aaa;
    }

    public int getBbb() {
        return bbb;
    }

    public void setBbb(int bbb) {
        this.bbb = bbb;
    }

    public float getCcc() {
        return ccc;
    }

    public void setCcc(float ccc) {
        this.ccc = ccc;
    }
}

Test.java

class Test {
    public static void main(String[] args) {
        A oa = new A();
        oa.setName("wei.hu");
        oa.setGender("man");

        List hobbys = Lists.newArrayList("basketball", "pingpang", "footbool");
        oa.setHobby(hobbys);

        Map map1 = Maps.newHashMap();
        map1.put("key1", "value1");
        map1.put("key2", "value2");
        map1.put("key3", "value3");

        Map map2 = Maps.newHashMap();
        map2.put("key1", "value1");
        map2.put("key2", "value2");
        map2.put("key3", "value3");

        List> mapList = Lists.newArrayList(map1, map2);
        oa.setXxx(mapList);

        B b = new B();
        b.setSa("sa");
        b.setSb(10086);

        List integerList = Lists.newArrayList(11,12,13,14);
        b.setIntegerList(integerList);

        List stringList = Lists.newArrayList("111", "222", "333");
        List stringList1 = Lists.newArrayList("000", "555");
        List stringList2 = Lists.newArrayList("qqq");
        List stringList3 = Lists.newArrayList("rrr", "ooo", "ppp", "iii");

        Map> stringListMap = Maps.newHashMap();
        stringListMap.put("key", stringList);
        stringListMap.put("key1", stringList1);
        stringListMap.put("key2", stringList2);
        stringListMap.put("key3", stringList3);

        b.setStringListMap(stringListMap);

        oa.setB(b);

        Map>>
        C c1 = new C("c1", 18, 3.14f);
        Map integerMap1 = Maps.newHashMap();
        integerMap1.put(001, c1);

        C c2 = new C("c2", 19, 3.15f);
        C c3 = new C("c3", 20, 3.16f);
        Map integerMap2 = Maps.newHashMap();
        integerMap2.put(002, c2);
        integerMap2.put(003, c3);

        C c4 = new C("c4", 10, 3.10f);
        C c5 = new C("c5", 11, 3.11f);
        C c6 = new C("c6", 12, 3.12f);
        Map integerMap3 = Maps.newHashMap();
        integerMap3.put(004, c4);
        integerMap3.put(005, c5);
        integerMap3.put(006, c6);

        List> mapList1 = Lists.newArrayList(integerMap1, integerMap2, integerMap3);


        C cc1 = new C("cc1", 180, 13.14f);
        Map integerMap4 = Maps.newHashMap();
        integerMap4.put(011, cc1);

        C cc2 = new C("cc2", 190, 13.15f);
        C cc3 = new C("cc3", 200, 13.16f);
        Map integerMap5 = Maps.newHashMap();
        integerMap5.put(012, cc2);
        integerMap5.put(013, cc3);

        List> mapLists2 = Lists.newArrayList(integerMap4, integerMap5);

        Map>> cListMap = Maps.newHashMap();
        cListMap.put(c1, mapList1);
        cListMap.put(cc1, mapLists2);

        oa.setbListMap(cListMap);



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

        A oa1 = JSON.parseObject(JSON.toJSONString(oa), A.class);
    }
}

是的,你没有看错,通过JSON.parseObject(String, Class)我们就可以将一个json字符串转换成一个足够复杂的javabean。

你可能感兴趣的:(学习日记)