解析json方式之net.sf.json
前面转载了json解析的技术:fastjson,今天说下另外一种技术。
下载地址
- 本次使用版本: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");
- //这个方法是将对象转为json对象(不论是将json字符串还是java对象都是这个方法)
- 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"));
- //将list集合转为json数组对象
- 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>
- */
解析json方式之net.sf.json的更多相关文章
- java解析JSON (使用net.sf.json)
例如JSON字符串str如下: { "data": [ { "basic_title": "运筹帷幄因 ...
- JSON数据转换之net.sf.json包的使用
转载 解析json之net.sf.json https://blog.csdn.net/itlwc/article/details/38442667 一.介绍 使用之前需要导入的jar包: json- ...
- json解析异常 - net.sf.json.JSONException: java.lang.reflect.InvocationTargetException
注:在项目中, 我使用原生的ajax请求数据的时候, JSONObject没能帮我解析, 当却不给我报错, 我是在junit单元测试中测试的时候, 发现的.发现好多时候, 特别是通过ajax请求, 不 ...
- java中转换json方式(JSONArray,JSONObject),json解析
package com.yunos.tv.video.resource.controller.web; import java.util.ArrayList; import java.util.Has ...
- Java中net.sf.json包关于JSON与对象互转的问题
在Web开发过程中离不开数据的交互,这就需要规定交互数据的相关格式,以便数据在客户端与服务器之间进行传递.数据的格式通常有2种:1.xml:2.JSON.通常来说都是使用JSON来传递数据.本文正是介 ...
- Java中net.sf.json包关于JSON与对象互转的坑
在Web开发过程中离不开数据的交互,这就需要规定交互数据的相关格式,以便数据在客户端与服务器之间进行传递.数据的格式通常有2种:1.xml:2.JSON.通常来说都是使用JSON来传递数据.本文正是介 ...
- net.sf.json.JSONObject 和org.json.JSONObject 的差别
http://my.oschina.net/wangwu91/blog/340721 net.sf.json.JSONObject 和org.json.JSONObject 的差别. 一.创建jso ...
- net.sf.json.JSONException: There is a cycle in the hierarchy!的解决办法
使用Hibernate manytoone属性关联主表的时候,如果使用JSONArray把pojo对象转换成json对象时,很容易出现循环的异常.解决的办法就是, 在转换json对象时忽略manyto ...
- net.sf.json 时间格式的转化
后台代码 //后台代码 response.setCharacterEncoding("UTF-8"); JsonConfig jsonConfig = new JsonConfig ...
随机推荐
- 一起来学习Shell脚本
Shell脚本 Shell脚本(shell script),是一种为shell编写的脚本程序. 大家所说的shell通常都是指的shell脚本,但其实shell与shell脚本是两个不同的概念.由于习 ...
- python,函数式编程
函数式编程: 特点:允许传递的参数是函数,且允许返回一个函数. 由于Python允许使用变量,因此,Python不是纯函数式编程语言,同样的输入可能输出不同,有副作用.纯函数式编程语言没有变量,输入和 ...
- XML文件中关键字自动提示和不全配置
一.获得mybatis-3-config.dtd.mybatis-3-mapper.dtd 这两个文件. 建立一个Maven的项目 在Pom.xml文件中的Mybatis jar包的下载设置(也可以从 ...
- struts2官方 中文教程 系列八:异常处理
在本教程中,我们将探讨如何启用Struts 2框架处理web应用程序生成的任何未捕获的异常.Struts 2提供了健壮的异常处理,包括能够自动记录任何未捕获的异常,并将用户重定向到错误web页面. 贴 ...
- 正则表达式 Pattern和Matcher
java.util.regex是一个用正则表达式所订制的模式来对字符串进行匹配工作的类库包. 1.简介: java.util.regex是一个用正则表达式所订制的模式来对字符串进行匹配工作的类库包. ...
- jmeter3.2版本如何进行webservice接口功能测试
jmeter3.2版本之后就没有SOAP/XML-RPC Request插件了,所以没办法直接进行webservice接口的测试. 原理上: Web service一般就是用SOAP协议通过HTTP来 ...
- Vm-Ubuntu下配置Qt开发环境
在昨天的Ubuntu换降下,安装Qt发现编译的时候是缺少opengl的 奈何找了好多方式都无法安装opengl 今天看到另一位大神写的,才发下自己找的还是有问题 大神帖子网址:http://blog. ...
- 自动化测试---mybatis的使用
mybatis如何实现了对数据库的操作: 1.通过Resources.getResourceAsReader()或者 Resources.getResourceAsStream()加载mybatis. ...
- Python 3基础教程18-获取用户键盘输入
有时候,我们需要获取用户的键盘输入的信息,然后得到信息,拿去做一些事情. 请看下面的demo.py # 练习如何通过键盘获取用户输入 x = input('What is your name?') p ...
- 第十七篇 Python函数之闭包与装饰器
一. 装饰器 装饰器:可以拆解来看,器本质就是函数,装饰就是修饰的意思,所以装饰器的功能就是为其他函数添加附加功能. 装饰器的两个原则: 1. 不修改被修饰函数的源代码 2. 不修改被修饰函数的调用方 ...