没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
JSON-lib 技术指南
Json-lib 可以将 Java 对象转成 json 格式的字符串,也可以将 Java 对象转换成 xml
格式的文档,同样可以将 json 字符串转换成 Java 对象或是将 xml 字符串转换成 Java
对象。
一、 准备工作
1、首先要去官方下载 json-lib 工具包
下载地址:
http://sourceforge.net/projects/json-lib/files/json-lib/json-
lib-2.4/
目前最新的是 2.4 的版本,本示例中使用的是 v2.3;json-lib 还需要以下依赖包:
jakarta commons-lang 2.5
jakarta commons-beanutils 1.8.0
jakarta commons-collections 3.2.1
jakarta commons-logging 1.1.1
ezmorph 1.0.6
官方网址:http://json-lib.sourceforge.net/
然后在工程中添加如下 jar 包:
当然你也可以用 2.4 的 json-lib 库
你可以在这里看看官方提供的示例:
http://json-lib.sourceforge.net/usage.html
由于本次介绍的示例需要 junit 工具,所以你还需要添加 junit 的 jar 文件,版本
是 4.8.2 版 本 的 , 下 载 地 址 : https://github.com/KentBeck/junit/
downloads
如果你 还 不 了 解 JSON 是什么? 那 么 你 应 该可以看 看 http://www.json.org/
json-zh.html
2、要转换的 JavaBean 的代码如下:
package com.hoo.entity;
public class Student {
private int id;
private String name;
private String email;
private String address;
private Birthday birthday;
//setter、getter
public String toString() {
return this.name + "#" + this.id + "#" + this.address + "#"
+ this.birthday + "#" + this.email;
}
}
Birthday.java
package com.hoo.entity;
public class Birthday {
private String birthday;
public Birthday(String birthday) {
super();
this.birthday = birthday;
}
//setter、getter
public Birthday() {}
@Override
public String toString() {
return this.birthday;
}
}
注意,上面的 getter、setter 方法省略了,自己构建下。
3、新建 JsonlibTest 测试类,基本代码如下:
package com.hoo.test;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONFunction;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;
import net.sf.json.util.PropertyFilter;
import net.sf.json.xml.XMLSerializer;
import org.apache.commons.beanutils.PropertyUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.hoo.entity.Birthday;
import com.hoo.entity.Student;
/**
* <b>function:</b> 用 json-lib
转换 java 对象到 JSON 字符串
* 读取 json
字符串到 java 对象,序列化 jsonObject 到 xml
* json-lib-version: json-lib-2.3-jdk15.jar
* 依赖包:
* commons-beanutils.jar
* commons-collections-3.2.jar
* ezmorph-1.0.3.jar
* commons-lang.jar
* commons-logging.jar
* @author hoojo
* @createDate Nov 28, 2010 2:28:39 PM
* @file JsonlibTest.java
* @package com.hoo.test
* @project WebHttpUtils
* @blog http://blog.csdn.net/IBM_hoojo
* @email hoojo_@126.com
* @version 1.0
*/
@SuppressWarnings({ "deprecation", "unchecked" })
public class JsonlibTest {
private JSONArray jsonArray = null;
private JSONObject jsonObject = null;
private Student bean = null;
@Before
public void init() {
jsonArray = new JSONArray();
jsonObject = new JSONObject();
bean = new Student();
bean.setAddress("address");
bean.setEmail("email");
bean.setId(1);
bean.setName("haha");
Birthday day = new Birthday();
day.setBirthday("2010-11-22");
bean.setBirthday(day);
}
@After
public void destory() {
jsonArray = null;
jsonObject = null;
bean = null;
System.gc();
}
public final void fail(String string) {
System.out.println(string);
}
public final void failRed(String string) {
System.err.println(string);
}
}
上面的 init 会在每个方法之前运行,destory 会在每个方法完成后执行。分别用到
了 junit 的@Before、@After 注解,如果你对 junit 的这些注解不是很了解,可以
看看 junit 官方的测试用例的 example 和 doc;
JSONObject 是将 Java 对象转换成一个 json 的 Object 形式,JSONArray 是将一
个 Java 对象转换成 json 的 Array 格式。
那什么是 json 的 Object 形式、Array 形式?
用通俗易懂的方法将,所谓的 json 的 Object 形式就是一个花括号里面存放的如
JavaMap 的键值对,如:{name:’hoojo’, age: 24};
那么 json 的 Array 形式呢?
就是中括号,括起来的数组。如:[ ‘json’, true, 22];
如果你还想了解更多 json 方面的知识,请看:
http://www.json.org/json-zh.html
除了上面的 JSONArray、JSONObject 可以将 Java 对象转换成 JSON 或是相反,将
JSON 字符 串转换成 Java 对象 , 还 有 一 个对象也可以完成上面的功能,它 就 是
JSONSerializer;下面我们就来看看它们是怎么玩转 Java 对象和 JSON 的。
二、 Java 对象序列化成 JSON 对象
1、将 JavaObject 转换吃 JSON 字符串
在 JsonlibTest 中添加如下代码:
/*=========================Java Object >>>> JSON String
===========================*/
/**
* <b>function:</b>转 Java Bean 对象到 JSON
* @author hoojo
* @createDate Nov 28, 2010 2:35:54 PM
*/
@Test
public void writeEntity2JSON() {
fail("==============Java Bean >>> JSON
Object==================");
fail(JSONObject.fromObject(bean).toString());
fail("==============Java Bean >>> JSON
Array==================");
fail(JSONArray.fromObject(bean).toString());//array 会在最外层套
上[]
fail("==============Java Bean >>> JSON Object
==================");
fail(JSONSerializer.toJSON(bean).toString());
fail("========================JsonConfig========================")
;
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.registerJsonValueProcessor(Birthday.class, new
JsonValueProcessor() {
public Object processArrayValue(Object value, JsonConfig
jsonConfig) {
if (value == null) {
return new Date();
}
return value;
}
public Object processObjectValue(String key, Object value,
剩余20页未读,继续阅读
资源评论
hoojo
- 粉丝: 1628
- 资源: 46
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 什么是移动应用开发-关于移动应用开发的相关介绍
- 第二组汇报 PPT.pptx
- 什么是大数据开发-相关介绍-关于大数据开发的相关介绍
- 什么是软件测试-相关介绍-关于软件测试的相关介绍
- 前端开发-什么是前端开发-关于前端开发的一些相关介绍
- Sora AI-关于文生视频的使用场景说明
- suno AI文生视频的相关教程和介绍使用
- 什么是后端开发-关于后端开发的一些小介绍分享
- Jurassic Pack Vol. II Dinosaurs 侏罗纪包卷恐龙二号Unity游戏模型资源unitypackage
- Jurassic Pack Vol. III Dinosaurs 侏罗纪包卷恐龙三号Unity游戏模型资源unitypackag
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功