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数据解析
您可以通过点击 右下角 的按钮 来对文章内容作出评价, 也可以通过左下方的 关注按钮 来关注我的博客的最新动态. 如果文章内容对您有帮助, 不要忘记点击右下角的 推荐按钮 来支持一下哦 如果您对文章内 ...
随机推荐
- Nuxt.js + koa2 入门
1. nuxt项目初始化 下面是使用 koa 模板方法初始化一个项目,使用该方法需要将 nuxt 的版本降至1.4.2: 官方 https://zh.nuxtjs.org/guide/installa ...
- stl源码剖析 详细学习笔记 空间配置器
//---------------------------15/04/05---------------------------- /* 空间配置器概述: 1:new操作包含两个阶段操作 1>调 ...
- c语言数字图像处理(二):图片放大与缩小-双线性内插法
图像内插 假设一幅大小为500 * 500的图像扩大1.5倍到750 * 750,创建一个750 * 750 的网格,使其与原图像间隔相同,然后缩小至原图大小,在原图中寻找最接近的像素(或周围的像素) ...
- CentOS7使用winbind加入AD
https://ishm.idv.tw/?p=336 CentOS 7 使用 winbind 加入 AD 需求:已經熟悉 CentOS 6 的 AD 加入方式,CentOS 7 已將 winbind ...
- 新手向:从不同的角度来详细分析Redis
最近对华为云分布式缓存产品Redis做了一些研究,于是整理了一些基本的知识拿出来与大家分享,首先跟大家分享的是,如何从不同的角度来详细使用Redis. 小编将从以下9个角度来进行详细分析,希望可以帮到 ...
- Hybrid APP基础篇(二)->Native、Hybrid、React Native、Web App方案的分析比较
说明 Native.Hybrid.React.Web App方案的分析比较 目录 前言 参考来源 前置技术要求 楔子 几种APP开发模式 概述 Native App Web App Hybrid Ap ...
- CSAPP lab2 二进制拆弹 binary bombs phase_5
给出对应于7个阶段的7篇博客 phase_1 https://www.cnblogs.com/wkfvawl/p/10632044.htmlphase_2 https://www.cnblogs. ...
- Notes of Daily Scrum Meeting(12.21)
今天的燃尽图把周六的进度加了进来,由于我的失误没有及时更新TFS,所以出现了一些错误,向大家道歉. 下面是今天的任务总结: 团队成员 今日团队工作 陈少杰 继续进行网络连接的调试 王迪 测试搜索的功能 ...
- NetFPGA Demo ——reference_nic_nf1_cml
NetFPGA Demo --reference_nic_nf1_cml 实验平台 OS:deepin 15.4 开发板:NetFPGA_1G_CML 实验过程 从NetFPGA-1G-CML从零开始 ...
- 《Toward an SDN-Enabled Big Data Platform for Social TV Analysis》--2015--Han Hu
<面向应用于社会TV分析的应用了SDN的大数据平台> Abstract social TV analytics 是什么,就是说很多TV观众在微博.微信和推特等这些地方分享他们的观感时,然后 ...