• json针对list map set 应用


    package JSONtest;
     
     
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
     
    import net.sf.json.JSONObject;
     
    public class Jsontest1 {
    public static void main(String args[]){
    //生成一个JSONObject对象
    String data = "{}";
    JSONObject jsonObj = JSONObject.fromObject(data);
     
    //1.put 基本类型值
    int i=0;
    for(i=0; i<5; i++){
    jsonObj.put("id"+i, i);
    }
    System.out.println(jsonObj.toString());
     
    //2.put 一个JSONObject对象
    JSONObject obj1 = JSONObject.fromObject(data);
    for(i=0; i<5; i++){
    obj1.put("child"+i, i);
    }
    jsonObj.put("JSONObject", obj1);
    System.out.println(jsonObj.toString());
     
    //3.put 数组
    String[] strArr = new String[]{"加","数","组"};
    jsonObj.put("arr", strArr);
    System.out.println(jsonObj.toString());
     
    // 4.put List
    List<String> list = new ArrayList<String>();
    list.add("加");
    list.add("列");
    list.add("表");
    jsonObj.put("list", list);
    System.out.println(jsonObj.toString());
     
    //5.put Set
    Set<String> set = new HashSet<String>();
    set.add("加");
    set.add("集");
    set.add("合");
    jsonObj.put("set", set);
    System.out.println(jsonObj.toString());
     
    //6.put Map
    Map<String,Object> map = new HashMap<String, Object>();
    map.put("map1", "加");
    map.put("map2", "映");
    map.put("map3", "射");
    jsonObj.put("map", map);
    System.out.println(jsonObj.toString());
     
    //7.putAll Map
    jsonObj.putAll(map);
    System.out.println(jsonObj.toString());
     
    //8.组合: (返回的结果value是一个对象数组)
    List<JSONObject> jlist = new ArrayList<JSONObject>();
    JSONObject j = null;
    for(i=0; i<3; i++){
    j = JSONObject.fromObject(data);
    j.put("aaaa"+i, i);
    j.put("bbbb"+i, i);
    j.put("cccc"+i, i);
    jlist.add(j);
    }
    jsonObj.put("succList", jlist);
    System.out.println(jsonObj.toString());
     
    //9.组合 : (返回的结果value是一个对象数组,对象的某个属性又是一个对象(嵌套))
    List<JSONObject> jlist1 = new ArrayList<JSONObject>();
    for(i=0; i<3; i++){
    j = JSONObject.fromObject(data);
    j.put("xxxx"+i, i);
    j.put("yyyy"+i, i);
    j.put("zzzz"+i, obj1);
    jlist1.add(j);
    }
    jsonObj.put("failList", jlist1);
    System.out.println(jsonObj.toString());
     
    //10.组合: 可以任意嵌套
    JSONObject superJson = JSONObject.fromObject(data);
    superJson.put("super", jsonObj);
    superJson.put("base", "base");
    System.out.println(superJson.toString());
    }
    }
    输出结果:
    {"id0":0,"id1":1,"id2":2,"id3":3,"id4":4}
    {"id0":0,"id1":1,"id2":2,"id3":3,"id4":4,"JSONObject":{"child0":0,"child1":1,"child2":2,"child3":3,"child4":4}}
    {"id0":0,"id1":1,"id2":2,"id3":3,"id4":4,"JSONObject":{"child0":0,"child1":1,"child2":2,"child3":3,"child4":4},"arr":["加","数","组"]}
    {"id0":0,"id1":1,"id2":2,"id3":3,"id4":4,"JSONObject":{"child0":0,"child1":1,"child2":2,"child3":3,"child4":4},"arr":["加","数","组"],"list":["加","列","表"]}
    {"id0":0,"id1":1,"id2":2,"id3":3,"id4":4,"JSONObject":{"child0":0,"child1":1,"child2":2,"child3":3,"child4":4},"arr":["加","数","组"],"list":["加","列","表"],"set":["合","加","集"]}
    {"id0":0,"id1":1,"id2":2,"id3":3,"id4":4,"JSONObject":{"child0":0,"child1":1,"child2":2,"child3":3,"child4":4},"arr":["加","数","组"],"list":["加","列","表"],"set":["合","加","集"],"map":{"map3":"射","map2":"映","map1":"加"}}
    {"id0":0,"id1":1,"id2":2,"id3":3,"id4":4,"JSONObject":{"child0":0,"child1":1,"child2":2,"child3":3,"child4":4},"arr":["加","数","组"],"list":["加","列","表"],"set":["合","加","集"],"map":{"map3":"射","map2":"映","map1":"加"},"map3":"射","map2":"映","map1":"加"}
    {"id0":0,"id1":1,"id2":2,"id3":3,"id4":4,"JSONObject":{"child0":0,"child1":1,"child2":2,"child3":3,"child4":4},"arr":["加","数","组"],"list":["加","列","表"],"set":["合","加","集"],"map":{"map3":"射","map2":"映","map1":"加"},"map3":"射","map2":"映","map1":"加","succList":[{"aaaa0":0,"bbbb0":0,"cccc0":0},{"aaaa1":1,"bbbb1":1,"cccc1":1},{"aaaa2":2,"bbbb2":2,"cccc2":2}]}
    {"id0":0,"id1":1,"id2":2,"id3":3,"id4":4,"JSONObject":{"child0":0,"child1":1,"child2":2,"child3":3,"child4":4},"arr":["加","数","组"],"list":["加","列","表"],"set":["合","加","集"],"map":{"map3":"射","map2":"映","map1":"加"},"map3":"射","map2":"映","map1":"加","succList":[{"aaaa0":0,"bbbb0":0,"cccc0":0},{"aaaa1":1,"bbbb1":1,"cccc1":1},{"aaaa2":2,"bbbb2":2,"cccc2":2}],"failList":[{"xxxx0":0,"yyyy0":0,"zzzz0":{"child0":0,"child1":1,"child2":2,"child3":3,"child4":4}},{"xxxx1":1,"yyyy1":1,"zzzz1":{"child0":0,"child1":1,"child2":2,"child3":3,"child4":4}},{"xxxx2":2,"yyyy2":2,"zzzz2":{"child0":0,"child1":1,"child2":2,"child3":3,"child4":4}}]}
    {"super":{"id0":0,"id1":1,"id2":2,"id3":3,"id4":4,"JSONObject":{"child0":0,"child1":1,"child2":2,"child3":3,"child4":4},"arr":["加","数","组"],"list":["加","列","表"],"set":["合","加","集"],"map":{"map3":"射","map2":"映","map1":"加"},"map3":"射","map2":"映","map1":"加","succList":[{"aaaa0":0,"bbbb0":0,"cccc0":0},{"aaaa1":1,"bbbb1":1,"cccc1":1},{"aaaa2":2,"bbbb2":2,"cccc2":2}],"failList":[{"xxxx0":0,"yyyy0":0,"zzzz0":{"child0":0,"child1":1,"child2":2,"child3":3,"child4":4}},{"xxxx1":1,"yyyy1":1,"zzzz1":{"child0":0,"child1":1,"child2":2,"child3":3,"child4":4}},{"xxxx2":2,"yyyy2":2,"zzzz2":{"child0":0,"child1":1,"child2":2,"child3":3,"child4":4}}]},"base":"base"}
  • 相关阅读:
    JS iframe给父类传值
    JS table分页
    JS 日历
    C# 创建文件夹
    post数据html数据获取危险处理办法
    SQL日期数据格式的处理
    sqlserver中计算结果保留小数
    页面元素的定位:getBoundingClientRect()和document.documentElement.scrollTop
    正则表达式 匹配中文,英文字母和数字及_的写法!同时控制长度
    GridPanel分页条插件
  • 原文地址:https://www.cnblogs.com/tuanz/p/8709205.html
Copyright © 2020-2023  润新知