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"}

json针对list map set 应用的更多相关文章

  1. json字符串转map

    <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</ar ...

  2. fastJson将json字符串转换为map

    public class JsonUtil { /** * 将json转化成map * @param jsonStr * @return */ public static Map<String, ...

  3. JSON字符串与Map互转

    //一.map转为json字符串 public static String map2jsonstr(Map<String,?> map){ return JSONObject.toJSON ...

  4. Json字符串转map集合

    第一步:在pom.xml中添加依赖; <dependency> <groupId>com.alibaba</groupId> <artifactId>f ...

  5. json字符串转Map、json数组

    json数组转map public static void main(String[] args){ String strArr = "[{\"0\":\"zh ...

  6. go语言之进阶篇json解析到map

    1.json解析到map(通过类型断言,找到值和value类型) 示例: package main import ( "encoding/json" "fmt" ...

  7. JSON字符串转换为Map

    本文是利用阿里巴巴封装的FastJSON来转换json字符串的.例子如下: package com.zkn.newlearn.json; import com.alibaba.fastjson.JSO ...

  8. [GO]json解析到map

    package main import ( "encoding/json" "fmt" ) var str string func main() { m := ...

  9. C++实现json字符串与map的转换

    开源资源库 jsoncpp-src-0.5.0.tar.gz:https://sourceforge.net/projects/jsoncpp/ jsoncpp-master.ziphttps://g ...

随机推荐

  1. JMeter Websocket 二进制Binary压力测试或接口测试

    背景 最近在做游戏项目,做好java服务端,需要本地调试Websock服务,并且是二进制binary形式传输,网上的很多都是text形式传输的测试页面,所以不符合要求.为了解决websocket的二进 ...

  2. linux监控工具audit

    audit是什么? audit是记录linux审计信息的内核模块. 他记录系统中的各种动作和事件,比如系统调用,文件修改,执行的程序,系统登入登出和记录所有系统中所有的事件.audit还可以将审计记录 ...

  3. Python练习题 009:水仙花数

    [Python练习题 009] 打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身.例如:153是一个"水仙花数& ...

  4. python中np.arrange用法

    转载:https://www.cnblogs.com/ghllfl/p/8487692.html np.arange()经常用,其用法总结如下: np.arange(0,60,2) 生成从0到60的步 ...

  5. JDBC Java 连接 MySQL 数据库

    MySQL 版本:Server version: 5.7.17-log MySQL Community Server (GPL) 用于测试的 MySQL 数据库:game 查看数据库中的表 mysql ...

  6. Ubuntu常用工具安装

    安装 aptitude 管理软件 $ sudo apt-get install aptitude 安装gdebi(安装deb包) # 安装: $ sudo apt install gdebi-core ...

  7. 为什么很多国内公司在做 AI 芯片?

    据网上搜到的新闻报道,截止2019年,已经有20家企业投入到 AI 芯片的研发中,其中有很多厂商的芯片已经流片甚至商用了.为何有这么多公司在做AI芯片呢?简单来讲就是四个字:有利可图.具体来说有以下三 ...

  8. 轻轻松松学CSS:Flex布局

     Flex布局就是"弹性布局",它可以简便.完整.响应式地实现各种页面布局.引入弹性布局的目的,当页面需要适应不同的屏幕大小确保元素拥有恰当的布局方式,对一个容器中的子元素进行排列 ...

  9. 在Windows7系统中设置虚拟内存大小

    当我们的电脑物理内存空间不够用时,操作系统就会自动从硬盘空间上分出一块空间来当内存使用,这就是虚拟内存.可以说虚拟内存是物理内存的补充,是备用的物理内存.一般来说,如果电脑里的程序不多,占用内存资源不 ...

  10. # vue 如何通过前端来导出excel表格

    在做一些简单的demo时,偶尔会遇到导出excel表格.如果请后端帮忙的话 比较浪费时间,那么前端如何导出excel表格,下面就来记录一下之前使用到的案例 一.安装依赖 npm i file-save ...