net.sf.json------json解析
下载地址
- 本次使用版本:http://sourceforge.net/projects/json-lib/files/json-lib/json-lib-1.1/
- 最新版本:http://sourceforge.net/projects/json-lib/files/json-lib/
使用net.sf.json需要导入的包
JSONObject
- package com.itlwc.test;
- import net.sf.json.JSONArray;
- import net.sf.json.JSONObject;
- public class Test {
- public static void main(String[] args) {
- // 创建JSONObject
- JSONObject jsonObject = new JSONObject();
- jsonObject.put("username", "lwc");
- jsonObject.put("password", "123");
- // 打印:1
- System.out.println(jsonObject);
- // 增加属性,打印:2
- jsonObject.element("sex", "男");
- System.out.println(jsonObject);
- // 根据key返回,打印:3
- System.out.println(jsonObject.get("sex"));
- // 判读输出对象的类型
- boolean isArray = jsonObject.isArray();
- boolean isEmpty = jsonObject.isEmpty();
- boolean isNullObject = jsonObject.isNullObject();
- // 打印:4
- System.out.println("是否数组:" + isArray + " 是否空:" + isEmpty + " 是否空对象:"
- + isNullObject);
- // 把JSONArray增加到JSONObject中
- JSONArray jsonArray = new JSONArray();
- jsonArray.add(0, "lwc");
- jsonArray.add(1, "nxj");
- // 开始增加
- jsonObject.element("student", jsonArray);
- // 打印:5
- System.out.println(jsonObject);
- }
- }
- /*
- 打印结果
- {"username":"lwc","password":"123"}
- {"username":"lwc","password":"123","sex":"男"}
- 男
- 是否为数组:false 是否为空:false 是否为空对象:false
- {"username":"lwc","password":"123","sex":"男","student":["lwc","nxj"]}
- */
JSONArray
- package com.itlwc.test;
- import net.sf.json.JSONArray;
- import net.sf.json.JSONObject;
- public class Test {
- public static void main(String[] args) {
- //创建JSONArray
- JSONArray jsonArray = new JSONArray();
- jsonArray.add(0, "lwc");
- jsonArray.add(1, "nxj");
- jsonArray.element("mxj");
- //打印:1
- System.out.println(jsonArray);
- //根据下标返回,打印:2
- System.out.println(jsonArray.get(0));
- //根据下标设置新值,打印:3
- jsonArray.set(0, "itlwc");
- System.out.println(jsonArray);
- //把JSONObject放入到JSONArray中
- JSONObject jsonObject = new JSONObject();
- jsonObject.put("username", "lwc");
- jsonObject.put("password", "123");
- //开始增加,打印:4
- jsonArray.add(jsonObject);
- System.out.println(jsonArray);
- //遍历,打印:5
- for(int i=0;i<jsonArray.size();i++){
- System.out.print(jsonArray.get(i)+"\t");
- }
- }
- }
- /*
- 打印结果
- ["lwc","nxj","mxj"]
- lwc
- ["itlwc","nxj","mxj"]
- ["itlwc","nxj","mxj",{"username":"lwc","password":"123"}]
- itlwc nxj mxj {"username":"lwc","password":"123"}
- */
JavaBean与json字符串互转
- package com.itlwc.test;
- import net.sf.json.JSONObject;
- import com.itlwc.entity.Student;
- public class Test {
- public static void main(String[] args) {
- // JavaBean对象转json字符串
- Student stu1 = new Student("lwc", "111111");
- JSONObject jsonObject = JSONObject.fromObject(stu1);
- System.out.println(jsonObject);
- // json字符串转JavaBean
- String jsondata = "{\"password\":\"111111\",\"username\":\"lwc\"}";
- JSONObject jsonObject1 = JSONObject.fromObject(jsondata);
- Student stu2 = (Student) JSONObject.toBean(jsonObject1, Student.class);
- System.out.println(stu2);
- }
- }
- /*
- 打印结果:
- {"password":"111111","username":"lwc"}
- 用户: lwc 密码:111111
- */
List与json字符串互转
- package com.itlwc.test;
- import java.util.ArrayList;
- import java.util.List;
- import net.sf.json.JSONArray;
- import net.sf.json.JSONObject;
- import com.itlwc.entity.Student;
- public class Test {
- public static void main(String[] args) {
- // List转json字符串
- List list = new ArrayList();
- list.add(new Student("lwc", "111111"));
- list.add(new Student("nxj", "222222"));
- JSONArray jsonArray = JSONArray.fromObject(list);
- System.out.println(jsonArray);
- // json字符串转List
- List list1 = new ArrayList();
- String jsondata = "[{\"password\":\"111111\",\"username\":\"lwc\"},{\"password\":\"222222\",\"username\":\"nxj\"}]";
- JSONArray jsonArray1 = JSONArray.fromObject(jsondata);
- for (int i = 0; i < jsonArray1.size(); i++) {
- JSONObject jsonObject2 = jsonArray1.getJSONObject(i);
- Student stu2 = (Student) JSONObject.toBean(jsonObject2,
- Student.class);
- list1.add(stu2);
- }
- System.out.println(list1);
- }
- }
- /*
- 打印结果:
- [{"password":"111111","username":"lwc"},{"password":"222222","username":"nxj"}]
- [用户: lwc 密码:111111, 用户: nxj 密码:222222]
- */
Map与json字符串互转
- package com.itlwc.test;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Set;
- import net.sf.json.JSONObject;
- import com.itlwc.entity.Student;
- public class Test {
- public static void main(String[] args) {
- // Map转json字符串
- Map map = new HashMap();
- map.put("1", new Student("lwc", "111111"));
- map.put("2", new Student("nxj", "222222"));
- JSONObject jsonMap = JSONObject.fromObject(map);
- System.out.println(jsonMap);
- // json字符串转Map
- String jsondata = "{\"2\":{\"password\":\"222222\",\"username\":\"nxj\"},\"1\":{\"password\":\"111111\",\"username\":\"lwc\"}}";
- Map map1 = (Map) JSONObject.fromObject(jsondata);
- Set set = map1.keySet();
- Iterator ite = set.iterator();
- while (ite.hasNext()) {
- String key = (String) ite.next();
- JSONObject jsonObject = JSONObject.fromObject(map1.get(key));
- Student stu = (Student) JSONObject
- .toBean(jsonObject, Student.class);
- System.out.println(key + " " + stu);
- }
- }
- }
- /*
- 打印结果:
- {"2":{"password":"222222","username":"nxj"},"1":{"password":"111111","username":"lwc"}}
- 2 用户: nxj 密码:222222
- 1 用户: lwc 密码:111111
- */
JSONArray与List互转
- package com.itlwc.test;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- import net.sf.json.JSONArray;
- import net.sf.json.JsonConfig;
- import com.itlwc.entity.Student;
- public class Test {
- public static void main(String[] args) {
- //List转型JSONArray
- List<Student> list = new ArrayList<Student>();
- list.add(new Student("lwc", "111111"));
- list.add(new Student("nxj", "222222"));
- JSONArray jsonArray = JSONArray.fromObject(list);
- System.out.println(jsonArray.toString());
- //JSONArray转型List
- List<Student> list2 = JSONArray.toList(jsonArray, new Student(), new JsonConfig());
- Iterator<Student> ite = list2.iterator();
- while(ite.hasNext()){
- Student stu =ite.next();
- System.out.println(stu);
- }
- }
- }
- /*
- 打印结果
- [{"password":"111111","username":"lwc"},{"password":"222222","username":"nxj"}]
- 用户: lwc 密码:111111
- 用户: nxj 密码:222222
- */
JSONArray与数组互转
- package com.itlwc.test;
- import net.sf.json.JSONArray;
- public class Test {
- public static void main(String[] args) {
- // Java数组转JSONArray
- boolean[] boolArray = new boolean[] { true, false, true };
- JSONArray jsonArray = JSONArray.fromObject(boolArray);
- System.out.println(jsonArray.toString());
- // JSONArray转Java数组
- Object obj[] = jsonArray.toArray();
- for (Object o : obj) {
- System.out.print(o + " ");
- }
- }
- }
- /*
- 打印结果 :
- [true,false,true]
- true false true
- */
XML与JSON互转
需要导入xom-1.1.jar
- package com.itlwc.test;
- import net.sf.json.JSON;
- import net.sf.json.JSONObject;
- import net.sf.json.xml.XMLSerializer;
- public class Test {
- public static void main(String[] args) throws Exception {
- // XML转JSON
- String xml = "<root>" + "<name type='type'>zhaipuhong</name>"
- + "<gender>male</gender>" + "<birthday>" + "<year>1970</year>"
- + "<month>12</month>" + "<day>17</day>" + "</birthday>"
- + "</root>";
- XMLSerializer xmlSerializer = new XMLSerializer();
- JSON json = xmlSerializer.read(xml);
- System.out.println(json.toString(2));
- // JSON转XML
- String jsondata = "{\"root\":{" + "\"name\":\"zhaipuhong\","
- + "\"gender\":\"male\"," + "\"birthday\":{"
- + "\"year\":\"1970\"," + "\"month\":\"12\"," + "\"day\":\"17\""
- + "}" + "}" + "}";
- JSONObject jsonObject = JSONObject.fromObject(jsondata);
- String xmlstr = new XMLSerializer().write(jsonObject);
- System.out.println(xmlstr);
- }
- }
- /*
- 打印结果:
- {
- "name": "zhaipuhong",
- "gender": "male",
- "birthday": {
- "year": "1970",
- "month": "12",
- "day": "17"
- }
- }
- <?xml version="1.0" encoding="UTF-8"?>
- <o>
- <root class="object">
- <birthday class="object">
- <day type="string">17</day>
- <month type="string">12</month>
- <year type="string">1970</year>
- </birthday>
- <gender type="string">male</gender>
- <name type="string">zhaipuhong</name>
- </root>
- </o>
- */
net.sf.json------json解析的更多相关文章
- java,jquery对json的解析
json常用于浏览器对服务器的数据传递,所以,我们会经常在浏览器和服务器段对json进行封装和拆装,下面对这些进行简单介绍吧 1,服务器端,也就是java方面,我们用的是 net.sf.json-li ...
- plist文件、NSUserDefault 对文件进行存储的类、json格式解析
========================== 文件操作 ========================== Δ一 .plist文件 .plist文件是一个属性字典数组的一个文件: .plis ...
- [开源 .NET 跨平台 数据采集 爬虫框架: DotnetSpider] [四] JSON数据解析
[DotnetSpider 系列目录] 一.初衷与架构设计 二.基本使用 三.配置式爬虫 四.JSON数据解析与配置系统 场景模拟 假设由于漏存JD SKU对应的店铺信息.这时我们需要重新完全采集所有 ...
- JSON数据解析 基础知识及链接收集
JSON数据解析学习 JSON介绍 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式. JSON 是存储和交换文本信息的语法.类似 XML.但是JSON 比 ...
- 浅谈JSON数据解析方法
JSON数据解析 JSON是什么?? 如何把JSON数据解析出来 如何把一个字典转换为JSON JSON详细介绍 JSON(JavaScript Object Notation) 是一种轻量级的数据交 ...
- JSON数据解析(转)
JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种理想的数据交换格式. 本文将主要介绍在Android ...
- JSON数据解析(GSON方式) (转)
JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种理想的数据交换格式. 在上一篇博文<Andro ...
- iOS - JSON 数据解析
iOS - JSON 数据解析 前言 NS_CLASS_AVAILABLE(10_7, 5_0) @interface NSJSONSerialization : NSObject @availab ...
- JSON.parse解析单引号名值对报错
今天整理代码的时候发现JSON.pare解析时会报了一个错误,而且很难找原因,调试了几分钟没有成功,猜测可能是单双引号引起的错误.修改了单双引号后程序正常运行了,现在记录下这个bug. 关于JSON. ...
- Android系列---JSON数据解析
您可以通过点击 右下角 的按钮 来对文章内容作出评价, 也可以通过左下方的 关注按钮 来关注我的博客的最新动态. 如果文章内容对您有帮助, 不要忘记点击右下角的 推荐按钮 来支持一下哦 如果您对文章内 ...
随机推荐
- Jenkins控制台输出乱码
一.问题详情 jenkins构建mav任务,在控制台显示乱码: 二.原因分析 1. 查看系统编码和tomcat的编码都正常 # grep encoding /usr/local/tomcat/conf ...
- Linux每天一个命令:iperf
iperf命令 Iperf 是一个网络性能测试工具.Iperf可以测试最大TCP和UDP带宽性能,具有多种参数和UDP特性,可以根据需要调整,可以报告带宽.延迟抖动和数据包丢失.下载地址:https: ...
- kubeadm安装kubernetes 1.13.1集群完整部署记录
k8s是什么 Kubernetes简称为k8s,它是 Google 开源的容器集群管理系统.在 Docker 技术的基础上,为容器化的应用提供部署运行.资源调度.服务发现和动态伸缩等一系列完整功能,提 ...
- 统计学习方法c++实现之六 支持向量机(SVM)及SMO算法
前言 支持向量机(SVM)是一种很重要的机器学习分类算法,本身是一种线性分类算法,但是由于加入了核技巧,使得SVM也可以进行非线性数据的分类:SVM本来是一种二分类分类器,但是可以扩展到多分类,本篇不 ...
- 《Linux内核分析》第一周学习报告
第一周:计算机是如何工作的 姓名:王玮怡 学号:20135116 第一节 存储程序计算机工作模型(冯诺依曼体系结构) IP指向的内存地址,取指令执行,完成后,IP值自加一,取下一条指令再执行. AP ...
- Linux 系统目录
/ 根目录 /bin 存放必要的命令 /boot 存放内核以及启动所需的文件等 /dev 存放设备文件 /etc 存放系统的配置文件 /home 用户文件的主目录,用户数据存放在其主目录中 /lib ...
- Linux 基础一(系统分区、格式化与挂载)
1.Linux 基础之系统分区与格式化 讲分区之前,先说一下硬盘结构:硬盘(机械)的横截面是一个圆,并且被分成等大小的扇区,每个扇区的大小是 512Byte,其中有 446Byte 被用来存储启动信息 ...
- Beta阶段敏捷冲刺②
1.提供当天站立式会议照片一张. 每个人的工作 (有work item 的ID),并将其记录在码云项目管理中: 1.1昨天已完成的工作. 姓名 昨天已完成的工作 徐璐琳 完成设置界面的排版 祁泽文 实 ...
- php artisan 命令列表
php artisan 命令列表 命令获取 上面的翻译内容 命令 说明 备注 php artisan make:resource ? 创建api返回格式化资源 >=5.4版本可用 php ar ...
- Only IPV6 下 使用MSDTC ping 之后 出现 找不到主机的问题
1. 很奇怪 使用两个虚拟机 进行IPV6测试, 需要验证MSDTC. app的机器名 Win12r2update DB的机器名 Win12r2UpdateDB 2. 在使用ipv4 以及 不进行ho ...