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. Java单例模式之最优解分析【为何说是最优解】

    代码如下 /** * * @ClassName: SingletionStaticInner * @Description: TODO[单例模式之最优解] * @author shundong.wu ...

  2. MySQL启动出现The server quit without updating PID file错误解决办法

    启动mysql服务的时候报下面这个错: 之间网上搜了各种办法,有重新初始化的(这怎么可能,里面还有数据...),有修改启动脚本的等等,但是都没用. 其实解决办法非常简单粗暴,那就是把/etc/my.c ...

  3. C#中 将图片保存到Sql server 中

    private void Form1_Load(object sender, EventArgs e) { #region 保存数据库 string url = @"C:\Users\Adm ...

  4. Flask-Web开发(第一部分)

    1.Jinja2变量过滤器: safe 渲染值时不转义capitalize 把值的首字母转换成大写,其他字母转换成小写lower 把值转换成小写形式upper 把值转换成大写形式title 把值中每个 ...

  5. thrift安装及python和c++版本调试

    一.安装过程 1.安装依赖库 ]# yum install boost-devel-static libboost-dev libboost-test-dev libboost-program-opt ...

  6. VS2015创建ASP.NET应用程序描述

    你的 ASP.NET 应用程序 恭喜! 你已创建了一个项目 此应用程序包含: 显示“主页”.“关于”和“联系方式”之间的基本导航的示例页 使用 Bootstrap 进行主题定位 身份验证,如果选择此项 ...

  7. AttributeError: 'module' object has no attribute 'enableTrace'

    Traceback (most recent call last): File "Long-lived-connection.py", line 29, in <module ...

  8. sed 删除文本

    sed删除文本命令可以将指定行或指定行范围进行删除,sed编辑命令的删除文本符号为 d,删除文本的格式为. [ sed]$ cat input [ sed]$ sed '/8/d' input 删除最 ...

  9. QT学习之如何在QToolBar中添加带图标的QToolButton并设置图标大小

    在网上查到了三种方法,找到一种比较好理解的. 使用QIcon类: QToolButton *toolBtn1 = new QToolButton(this); //创建QToolButton tool ...

  10. C# 登陆验证码工具类VerifyCode

    using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; ...