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的学习的更多相关文章

  1. JSON 教程学习进度备忘

    书签:跳过:另外跳过的内容有待跟进 __________________ 学习资源:W3School. _________________ 跳过的内容: 1. ______________ 知识点:1 ...

  2. Json.Net学习笔记

    http://www.cnblogs.com/xiaojinhe2/archive/2011/10/28/2227789.html Newtonsoft.Json(Json.Net)学习笔记 http ...

  3. Newtonsoft.Json(Json.Net)学习

    转自原文 Newtonsoft.Json(Json.Net)学习笔记 Newtonsoft.Json,一款.NET中开源的Json序列化和反序列化类库.软件下载地址: http://www.newto ...

  4. JSON的学习与使用

    .katex { display: block; text-align: center; white-space: nowrap; } .katex-display > .katex > ...

  5. Android之Json的学习

    json数据包含json对象,json数组,对象是{ },数组是[ ], 数组里面还可以包含json对象,json对象之间是用逗号(,)隔开 形式如下: { "languages" ...

  6. Newtonsoft.Json(Json.Net)学习笔记

    Newtonsoft.Json 在Vs2013中就有自带的: 下面是Json序列化和反序列化的简单封装: /// <summary> /// Json帮助类 /// </summar ...

  7. JSON入门学习

    JSON是一种与开发语言无关的轻量级的数据格式(JavaScript Object Notation) 优点:易于阅读和编写,易于程序解析和生产 JSON数据格式中没有日期及时间的数据格式的.一般直接 ...

  8. json官方学习档案

    项目经常用json开发,但说实话,对json了解的一直不深入.今天看了下json的官方资料,明了很多. json官方网址:http://www.json.org/json-zh.html JSON(J ...

  9. Newtonsoft.Json(Json.Net)学习笔记-高级使用(转)

    1.忽略某些属性 2.默认值的处理 3.空值的处理 4.支持非公共成员 5.日期处理 6.自定义序列化的字段名称 7.动态决定属性是否序列化 8.枚举值的自定义格式化问题 9.自定义类型转换 10.全 ...

  10. Newtonsoft.Json(Json.Net)学习笔记(转)

    概述 Newtonsoft.Json,一款.NET中开源的Json序列化和反序列化类库,通过Nuget获取.(查看原文) 下面是Json序列化和反序列化的简单封装: /// <summary&g ...

随机推荐

  1. 半导体知识讲解:IC基础知识及制造工艺流程

    本文转载自微信公众号 - 中国半导体论坛  , 链接 https://mp.weixin.qq.com/s/VhCsVGyEDrgc2XJ0jxLvaA

  2. js数字串传参时变科学计数法

    例1:onclick=channel_info_listFt(\"'+val.gid+'\",'+val.deviceIdOwner+','+val.gname+') 当长度过长的 ...

  3. 更改MySQL密码

    #安装MySQL5.7参考:https://blog.csdn.net/qq_23033339/article/details/80872136#MYSQL的基础操作参考:https://www.cn ...

  4. word20170103除了busy,忙的10种英语说法!

    年前,忙死啦!除了busy,这些说法更地道.更形象! #1 I'm swamped! Swamp: 沼泽“忙死了”最形象.最对应的英语说法:It's the end of the year. I'm ...

  5. python epoll方式tcp连接回发消息

    # -*- coding:utf-8 -*- import socket import select class testserver(): def __init__(self): self.serv ...

  6. sql选择

    关系型数据库遵循ACID规则 1.A (Atomicity) 原子性 原子性很容易理解,也就是说事务里的所有操作要么全部做完,要么都不做,事务成功的条件是事务里的所有操作都成功,只要有一个操作失败,整 ...

  7. 迭代和JDB

    迭代和JDB 使用C(n,m)=C(n-1,m-1)+C(n-1,m)公式进行递归编程实现求组合数C(m,n)的功能. 源代码 public class Combination { public st ...

  8. ZOJ1008

    题目: ZOJ 1008 分析: 重排矩阵, 虽然题目给的时间很多, 但是要注意剪枝, 把相同的矩阵标记, 在搜索时可以起到剪枝效果. Code: #include <bits/stdc++.h ...

  9. Windows【端口被占用,杀死想啥的端口】

    windows 两步方法 netstat -ano | findstr "8080" taskkill /pid 4136-t -f linux 两步方法 ps -ef | gre ...

  10. SQLAlchemy 使用(二)表关联

    前言 在上一章中我们介绍了 SQLAlchemy 建立基本表,但是一般情况下,表之间是有关联的,比如 一对一/一对多/多对多,当然 SQLAlchemy 是支持建立model时指定关系的 正文 多对一 ...