Json的学习
json的简介
Json是项目中常用的一种,数据格式简单,易于读写,格式都是压缩的,占用带宽小,轻量级,支持多种语言,可以直接为服务器代码使用。
json常用支持的转化,(map集合,字符串,还有对象)
1 map集合转为json数据
//将map集合转化为json
public static void test1(){ Map<String,String> map=new HashMap<>(); map.put("name", "zhangsan");
map.put("age", "23");
map.put("nickname", "xiaozhang"); JSONObject jsonObject=new JSONObject(map);
System.out.println(jsonObject); }
2.对象转为json数据
// 将对象转化为json
public static void test2(){ Student student=new Student(1001,"张三",23); JSONObject jsonObject=new JSONObject(student); System.out.println(jsonObject);
}
3.字符串转为json
// 将字符串转为json
public static void test3(){ String str="{\"name\":\"李四\",\"age\":23,\"clas\":\"后台班\"}"; JSONObject jsonObject=new JSONObject(str); System.out.println(jsonObject);
}
二. 将json文件转化输出到控制台,也可以使用commons.io这个工具
InputStream in=super.getClass().getClassLoader().getResourceAsStream("test/stu.json");
//
// byte[] bs=new byte[1024];
//
// int len=-1;
// StringBuffer sb=new StringBuffer();
// while((len=in.read(bs))!=-1){
//// byte-->string
// String str=new String(bs,0,len);
// sb.append(str);
//
// }
//// Stringbuffer-->json
// String s=sb.toString();
使用commons.io一句话就可以实现将json文件转为string
String str=FileUtils.readFileToString(new File("E:\\eclipseWorkspace\\test\\src\\test\\stu.json"));
三、使用Java代码可以生成一个json文件
// 生成json文件
public void test5() throws IOException{
Map<String, Student> map=new HashMap<>(); Student stu1=new Student(1001,"张三",23);
Student stu2=new Student(1002,"李四",24);
Student stu3=new Student(1003,"王五",25); map.put("lxl", stu1);
map.put("zxl", stu2);
map.put("wxl", stu3);
// map-->json
JSONObject jsonObject=new JSONObject(map);
// 生成json文件
Writer writer =new FileWriter("E:\\eclipseWorkspace\\test\\src\\test\\student.json"); jsonObject.write(writer);
writer.close();
}
四、jsonArray的介绍(json数组)
一般形如 [{...}, {....},{....}]此种类型称为json数组
四、1 可以将字符串转为json数组
public void test6(){
String str=
"[{\"name\":\"张三\",\"age\":23},{\"class\":\"lxl\",\"num\":12},{\"school\":\"sun\",\"addr\":\"sh\"}]";
net.sf.json.JSONArray json=new net.sf.json.JSONArray();
json=json.fromObject(str);
System.out.println(json);
}
四 2 json-lib包的使用,导入jar包
核心代码:JsonArray json=new JsonArray;
json =fromObject(Object o);
实例代码 1.将map集合转为json数组
public void test7(){
Map<String, Student> map=new HashMap<>();
Student stu1=new Student(1001,"张三",23);
Student stu2=new Student(1002,"李四",24);
Student stu3=new Student(1003,"王五",25);
map.put("lxl", stu1);
map.put("zxl", stu2);
map.put("wxl", stu3);
net.sf.json.JSONArray json=new net.sf.json.JSONArray();
json=json.fromObject(map);
System.out.println(json);
}
五、使用jsonArray转为map
思路:先从jsonArray中获取每一个json,获取json的键值对,json.get(i)获取每一个json,强转为jsonObject对象,jsonObject.keySet()可以获取json的所有的键的值,jsonObject.get(key)获取value的值,map.put重新装入map集合中。
public void test8(){
String str=
"[{\"name\":\"张三\",\"age\":23},{\"class\":\"lxl\",\"num\":12},{\"school\":\"sun\",\"addr\":\"sh\"}]";
net.sf.json.JSONArray json=new net.sf.json.JSONArray();
json=json.fromObject(str);
// 从jsonarray中获取每一个json
for (int i=0; i<json.size() ;i++) {
Object o=json.get(i);//获取每一个json
net.sf.json.JSONObject jsonObject=(net.sf.json.JSONObject) o;
// 获取json的key/value
Map<String, Object> map=new HashMap<>();
Set<String> keys= jsonObject.keySet();//每个json的所有的key
for (String key : keys) {
Object value=jsonObject.get(key);
map.put(key, value);
}
System.out.println(map);
}
Json的学习的更多相关文章
- JSON 教程学习进度备忘
书签:跳过:另外跳过的内容有待跟进 __________________ 学习资源:W3School. _________________ 跳过的内容: 1. ______________ 知识点:1 ...
- Json.Net学习笔记
http://www.cnblogs.com/xiaojinhe2/archive/2011/10/28/2227789.html Newtonsoft.Json(Json.Net)学习笔记 http ...
- Newtonsoft.Json(Json.Net)学习
转自原文 Newtonsoft.Json(Json.Net)学习笔记 Newtonsoft.Json,一款.NET中开源的Json序列化和反序列化类库.软件下载地址: http://www.newto ...
- JSON的学习与使用
.katex { display: block; text-align: center; white-space: nowrap; } .katex-display > .katex > ...
- Android之Json的学习
json数据包含json对象,json数组,对象是{ },数组是[ ], 数组里面还可以包含json对象,json对象之间是用逗号(,)隔开 形式如下: { "languages" ...
- Newtonsoft.Json(Json.Net)学习笔记
Newtonsoft.Json 在Vs2013中就有自带的: 下面是Json序列化和反序列化的简单封装: /// <summary> /// Json帮助类 /// </summar ...
- JSON入门学习
JSON是一种与开发语言无关的轻量级的数据格式(JavaScript Object Notation) 优点:易于阅读和编写,易于程序解析和生产 JSON数据格式中没有日期及时间的数据格式的.一般直接 ...
- json官方学习档案
项目经常用json开发,但说实话,对json了解的一直不深入.今天看了下json的官方资料,明了很多. json官方网址:http://www.json.org/json-zh.html JSON(J ...
- Newtonsoft.Json(Json.Net)学习笔记-高级使用(转)
1.忽略某些属性 2.默认值的处理 3.空值的处理 4.支持非公共成员 5.日期处理 6.自定义序列化的字段名称 7.动态决定属性是否序列化 8.枚举值的自定义格式化问题 9.自定义类型转换 10.全 ...
- Newtonsoft.Json(Json.Net)学习笔记(转)
概述 Newtonsoft.Json,一款.NET中开源的Json序列化和反序列化类库,通过Nuget获取.(查看原文) 下面是Json序列化和反序列化的简单封装: /// <summary&g ...
随机推荐
- python之路(5)文件操作(open)
目录 前言 文件的打开模式 文件句柄的方法 seek()方法介绍 前言 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 f = open('demo.txt','r',e ...
- 【C++笔记】explicit 指定符
用于抑制构造函数的自动隐式转换. struct A { A(int) { } // 转换构造函数 A(int, int) { } // 转换构造函数 (C++11) operator bool() c ...
- Contest2195 - 2019-4-25 高一noip基础知识点 测试8 题解版
(因为david_alwal太懒了,所以本期题解作者为Th Au K,码风不同请自行适应) 传送门 T1 BFS?贪心?我也说不清 反正就是对每一个“#”搜一下他的旁边有没有“#”就行了 代码 T2 ...
- day 22 - 2 面向对象练习
练习一 在终端输出如下信息 小明,10岁,男,上山去砍柴小明,10岁,男,开车去东北小明,10岁,男,最爱大保健老李,90岁,男,上山去砍柴老李,90岁,男,开车去东北老李,90岁,男,最爱大保健老张 ...
- day 22 - 1 面向对象
面向对象 字典实现人狗大战 #定义角色 def Person(name,hp,aggr,sex): person = { 'name':name, 'hp':hp, 'aggr':aggr, 'sex ...
- Redis代码——Python篇
需要安装的库:redis import redis # 连接数据库 r = redis.StrictRedis(host="localhost", port=6379, passw ...
- shiro教程
ref https://www.jianshu.com/p/5a35d0100a71 https://www.jianshu.com/p/0366a1675bb6 https://blog.csdn. ...
- STL--hashtable
hashtable使用开链的方式,解决元素个数大于array容量的问题. 当两个不同元素hash得到相同的hash值时,此时我们使用bucket list来链接连个元素. hashtable迭代器必须 ...
- Mybatis操作oracle数据库的一些坑
oracle区分大小写,如果不想区分,必须要用引号包住 使用mybatis的generator生成的时候,建议使用全大写表,中间用下划线隔开,会生成驼峰字段 oracle没有空字符串的概念 写myba ...
- Django-F,Q查询,Templatetags,session,中间件
内容总览1.ORM的多对多的使用 1>语法与实例 2>聚合与分组 3>F与Q查询 4>事务2.模板之自定义 1>初始化 2>filter 3>si ...