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"}
json针对list map set 应用的更多相关文章
- json字符串转map
<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</ar ...
- fastJson将json字符串转换为map
public class JsonUtil { /** * 将json转化成map * @param jsonStr * @return */ public static Map<String, ...
- JSON字符串与Map互转
//一.map转为json字符串 public static String map2jsonstr(Map<String,?> map){ return JSONObject.toJSON ...
- Json字符串转map集合
第一步:在pom.xml中添加依赖; <dependency> <groupId>com.alibaba</groupId> <artifactId>f ...
- json字符串转Map、json数组
json数组转map public static void main(String[] args){ String strArr = "[{\"0\":\"zh ...
- go语言之进阶篇json解析到map
1.json解析到map(通过类型断言,找到值和value类型) 示例: package main import ( "encoding/json" "fmt" ...
- JSON字符串转换为Map
本文是利用阿里巴巴封装的FastJSON来转换json字符串的.例子如下: package com.zkn.newlearn.json; import com.alibaba.fastjson.JSO ...
- [GO]json解析到map
package main import ( "encoding/json" "fmt" ) var str string func main() { m := ...
- C++实现json字符串与map的转换
开源资源库 jsoncpp-src-0.5.0.tar.gz:https://sourceforge.net/projects/jsoncpp/ jsoncpp-master.ziphttps://g ...
随机推荐
- 容器云平台No.5~企业级私有镜像仓库Harbor V2.02
镜像仓库 仓库,顾名思义,就是存放东西的地方,Docker仓库,理所当然,就是存放docker镜像的地方了. Docker仓库分公有仓库和私有仓库.共有仓库有hub.docker.com.gcr.io ...
- ES6 常用总结——第三章(数组、函数、对象的扩展)
1.1. Array.from() Array.from方法用于将两类对象转为真正的数组:类似数组的对象(array-like object)和可遍历(iterable)的对象(包括ES6新增的数据结 ...
- 使用maven纯注解集成ssm
1.配置springMVC框架 第一步:导入包依赖 <!--配置springMVC--> <dependency> <groupId>javax.servlet.j ...
- LINQ中的OrderBy实现按照两个字段升序、降序排序操作
在公司或许有这种需求,先根据第一个某个字段按照升序排序,然后如果相同,在按照第二个某个字降序排序,我们该怎么去实现呢? 现在来教教大家分别使用Labmda和LINQ进行这种操作. 1.先按照第一个字段 ...
- flutter,跟着官网一步一步创建第一个flutter应用
创建第一个flutter 编辑器: vscode 一.创建flutter项目 1.启动vscode 2.按ctrl+shift+p打开命令面板 3.输入flutter 选择Flutter: New P ...
- Dynamically allocated memory 动态分配内存【malloc】Memory leaks 内存泄漏
内存泄露Memory leaks :没有指针指向原来a分配出来的那段空间了
- pytorch和tensorflow的爱恨情仇之基本数据类型
自己一直以来都是使用的pytorch,最近打算好好的看下tensorflow,新开一个系列:pytorch和tensorflow的爱恨情仇(相爱相杀...) 无论学习什么框架或者是什么编程语言,最基础 ...
- CF538B Quasi Binary 思维题
题目描述 给出一个数 \(n\),你需要将 \(n\) 写成若干个数的和,其中每个数的十进制表示中仅包含\(0\)和\(1\). 问最少需要多少个数 输入输出格式 输入格式: 一行 一个数 \(n(1 ...
- Unicode、UTF8、GB2312、ANSI
来源:https://blog.csdn.net/osanwenyu/article/details/48439461 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原 ...
- matlab中bitshift 将位移动指定位数
来源:https://ww2.mathworks.cn/help/matlab/ref/bitshift.html?searchHighlight=bitshift&s_tid=doc_src ...