Json_lib可以方便的将Java对象转成json格式的字符串,也可以将Java对象转换成xml格式的文档,同样可以将Json字符串转换成Java对象,或者将xml字符串转换成Java对象。
官网:http://json-lib.sourceforge.net/
JSON在线转换:http://json.parser.online.fr/
JSON教程:http://www.json.org/json-zh.html
官网上说明了json_lib还需要依赖的Jar包有:
注意这里如果jakarta commons-lang 使用3.1版本的会报:Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang.exception.NestableRuntimeException
所以这里还是使用2.6的吧
MyBean.java:
01 |
package com.iflytek.json;
|
03 |
import net.sf.json.JSONFunction;
|
06 |
* @author xudongwang 2012-1-15
|
08 |
* Email:xdwangiflytek@gmail.com
|
11 |
private String name = "json" ;
|
12 |
private int pojoId = 1 ;
|
13 |
private char [] options = new char [] { 'a' , 'f' };
|
14 |
private String func1 = "function(i){ return this.options[i]; }" ;
|
15 |
private JSONFunction func2 = new JSONFunction( new String[] { "i" },
|
16 |
"return this.options[i];" );
|
19 |
public String getName() {
|
23 |
public void setName(String name) {
|
27 |
public int getPojoId() {
|
31 |
public void setPojoId( int pojoId) {
|
35 |
public char [] getOptions() {
|
39 |
public void setOptions( char [] options) {
|
40 |
this .options = options;
|
43 |
public String getFunc1() {
|
47 |
public void setFunc1(String func1) {
|
51 |
public JSONFunction getFunc2() {
|
55 |
public void setFunc2(JSONFunction func2) {
|
JsonlibTest.java :
001 |
package com.iflytek.json;
|
003 |
import java.lang.reflect.InvocationTargetException;
|
004 |
import java.util.ArrayList;
|
005 |
import java.util.HashMap;
|
006 |
import java.util.List;
|
007 |
import java.util.Map;
|
009 |
import net.sf.json.JSONArray;
|
010 |
import net.sf.json.JSONObject;
|
012 |
import org.apache.commons.beanutils.PropertyUtils;
|
015 |
* @author xudongwang 2012-1-15
|
017 |
* Email:xdwangiflytek@gmail.com
|
019 |
public class JsonlibTest {
|
022 |
private JSONArray jsonArray = null ;
|
024 |
private JSONObject jsonObject = null ;
|
026 |
public static void main(String[] args) {
|
027 |
JsonlibTest json = new JsonlibTest();
|
038 |
public void ArrayToJSON() {
|
039 |
boolean [] boolArray = new boolean [] { true , false , true };
|
040 |
jsonArray = JSONArray.fromObject(boolArray);
|
041 |
System.out.println( "数组转JSON操作:" + jsonArray);
|
047 |
public void ListToJSON() {
|
048 |
List<String> list = new ArrayList<String>();
|
051 |
jsonArray = JSONArray.fromObject(list);
|
052 |
System.out.println( "集合转JSON操作:" + jsonArray);
|
058 |
public void MapsToJSON() {
|
059 |
Map<String, Object> map = new HashMap<String, Object>();
|
060 |
map.put( "name" , "json" );
|
061 |
map.put( "bool" , Boolean.TRUE);
|
062 |
map.put( "int" , new Integer( 1 ));
|
063 |
map.put( "arr" , new String[] { "a" , "b" });
|
064 |
map.put( "func" , "function(i){ return this.arr[i]; }" );
|
065 |
jsonObject = JSONObject.fromObject(map);
|
066 |
System.out.println( "Maps转JSON操作:" + jsonObject);
|
072 |
public void BeanToJSON() {
|
073 |
jsonObject = JSONObject.fromObject( new MyBean());
|
074 |
System.out.println( "Bean转JSON操作:" + jsonObject);
|
080 |
public void JSONToBean() {
|
082 |
String json = "{\"func1\":function(i){ return this.options[i]; },\"func2\":function(i){ return this.options[i]; },\"name\":\"json\",\"options\":[\"a\",\"f\"],\"pojoId\":1}" ;
|
083 |
jsonObject = JSONObject.fromObject(json);
|
084 |
Object bean = JSONObject.toBean(jsonObject);
|
085 |
System.out.println( "jsonStr:" + json);
|
087 |
System.out.println( "name:" + jsonObject.get( "name" ));
|
088 |
System.out.println( "name:"
|
089 |
+ PropertyUtils.getProperty(bean, "name" ));
|
090 |
System.out.println( "pojoId:" + jsonObject.get( "pojoId" ));
|
091 |
System.out.println( "pojoId:"
|
092 |
+ PropertyUtils.getProperty(bean, "pojoId" ));
|
093 |
System.out.println( "options:" + jsonObject.get( "options" ));
|
094 |
System.out.println( "options:"
|
095 |
+ PropertyUtils.getProperty(bean, "options" ));
|
096 |
System.out.println( "func1:" + jsonObject.get( "func1" ));
|
097 |
System.out.println( "func1:"
|
098 |
+ PropertyUtils.getProperty(bean, "func1" ));
|
099 |
System.out.println( "func2:" + jsonObject.get( "func2" ));
|
100 |
System.out.println( "func2:"
|
101 |
+ PropertyUtils.getProperty(bean, "func2" ));
|
102 |
} catch (IllegalAccessException e) {
|
104 |
} catch (InvocationTargetException e) {
|
106 |
} catch (NoSuchMethodException e) {
|
一、简介
json-lib是一个Java类库,提供将Java对象,包括beans,maps,collections,java arrays和xml等转换成JSON,或者反向转换的功能。
在使用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
并将这些jar包引入到Eclipse项目当中,即可调用。
在进行下面的代码演示之前,我们先将几个基本的类介绍一下:
01 |
<span style= "font-size:14px;" > public class MyBean {
|
02 |
private String id = null ;
|
03 |
private String name = null ;
|
04 |
private Date date = null ;
|
05 |
private List cardNum = null ;
|
06 |
private String[] cardType = { "身份证" , "银行卡" , "公车卡" };
|
08 |
public String getId() {
|
11 |
public void setId(String id) {
|
14 |
public String getName() {
|
17 |
public void setName(String name) {
|
20 |
public Date getDate() {
|
23 |
public void setDate(Date date) {
|
26 |
public List getCardNum() {
|
29 |
public void setCardNum(List cardNum) {
|
30 |
this .cardNum = cardNum;
|
32 |
public String[] getCardType() {
|
35 |
public void setCardType(String[] cardType) {
|
36 |
this .cardType = cardType;
|
01 |
<span style= "font-size:14px;" > public class Person {
|
02 |
private String name = null ;
|
05 |
public Person(String name){
|
08 |
public String getName() {
|
11 |
public void setName(String name) {
|
17 |
public class MyBeanWithPerson {
|
18 |
private List<Person> list = null ;
|
19 |
private Map<String,Person> map = null ;
|
20 |
public List getList() {
|
23 |
public void setList(List list) {
|
29 |
public void setMap(Map map) {
|
1.将json字符串转换成JSON,根据情况用JSONArray或JSONObject
1 |
<span style= "font-size:14px;" > public static void testJsonStrToJSON() {
|
2 |
JSONArray jsonArray = JSONArray.fromObject( "[\"json\",\"is\",\"easy\"]" );
|
3 |
System.out.println(jsonArray); |
01 |
<span style= "font-size:14px;" > public static void testBeadToJSON() {
|
02 |
MyBean bean = new MyBean();
|
05 |
bean.setDate( new Date());
|
07 |
List cardNum = new ArrayList();
|
11 |
cardNum.add( new Person( "test" ));
|
13 |
bean.setCardNum(cardNum); |
15 |
JSONObject jsonObject = JSONObject.fromObject(bean); |
16 |
System.out.println(jsonObject); |
1 |
<span style= "font-size:14px;" > public static void testArrayToJSON() {
|
2 |
boolean [] boolArray = new boolean [] { true , false , true };
|
3 |
JSONArray jsonArray = JSONArray.fromObject(boolArray);
|
4 |
System.out.println(jsonArray);
|
1 |
<span style= "font-size:14px;" > public static void testListToJSON() {
|
2 |
List list = new ArrayList();
|
5 |
JSONArray jsonArray = JSONArray.fromObject(list); |
6 |
System.out.println(jsonArray); |
7 |
} </span><span style= "font-family:Simsun;font-size:14px;" > </span>
|
01 |
<span style= "font-size:14px;" > public static void testMapToJSON() {
|
02 |
Map map = new HashMap();
|
03 |
map.put( "name" , "json" );
|
04 |
map.put( "bool" , Boolean.TRUE);
|
05 |
map.put( "int" , new Integer( 1 ));
|
06 |
map.put( "arr" , new String[] { "a" , "b" });
|
07 |
map.put( "func" , "function(i){ return this.arr[i]; }" );
|
09 |
JSONObject jsonObject = JSONObject.fromObject(map); |
10 |
System.out.println(jsonObject); |
01 |
<span style= "font-size:14px;" > public static void testJSONToObject() throws Exception {
|
03 |
String json = "{name=\"json\",bool:true,int:1,double:2.2,func:\"function(a){ return a; }\",array:[1,2]}" ;
|
04 |
JSONObject jsonObject = JSONObject.fromObject(json);
|
05 |
System.out.println(jsonObject);
|
08 |
Object bean = JSONObject.toBean(jsonObject);
|
09 |
System.out.println(PropertyUtils.getProperty(bean, "name" ));
|
10 |
System.out.println(PropertyUtils.getProperty(bean, "bool" ));
|
11 |
System.out.println(PropertyUtils.getProperty(bean, "int" ));
|
12 |
System.out.println(PropertyUtils.getProperty(bean, "double" ));
|
13 |
System.out.println(PropertyUtils.getProperty(bean, "func" ));
|
14 |
System.out.println(PropertyUtils.getProperty(bean, "array" ));
|
15 |
List arrayList = (List) JSONArray.toCollection(jsonObject.getJSONArray( "array" ));
|
16 |
for (Object object : arrayList) {
|
17 |
System.out.println(object);
|
19 |
} </span><span style= "font-family:Simsun;font-size:14px;" > </span>
|
7.将复合类型的JSON字符串转换成复合对象,包含List
01 |
<span style= "font-size:14px;" > public static void testJSONToBeanHavaList() {
|
02 |
String json = "{list:[{name:\"test1\"},{name:\"test2\"}]}" ;
|
03 |
Map classMap = new HashMap();
|
04 |
classMap.put( "list" , Person. class );
|
05 |
MyBeanWithPerson diyBean = (MyBeanWithPerson) JSONObject.toBean( |
06 |
JSONObject.fromObject(json), MyBeanWithPerson. class , classMap);
|
07 |
System.out.println(diyBean); |
09 |
List list = diyBean.getList(); |
10 |
for (Object o : list) {
|
11 |
if (o instanceof Person) {
|
12 |
Person p = (Person) o;
|
13 |
System.out.println(p.getName()); |
8.将复合类型的JSON字符串转换成复合对象,包含Map
01 |
<span style= "font-size:14px;" > public static void testJSONToBeanHavaMap() {
|
03 |
String json = "{list:[{name:\"test1\"},{name:\"test2\"}],map:{test1:{name:\"test1\"},test2:{name:\"test2\"}}}" ;
|
04 |
Map classMap = new HashMap();
|
05 |
classMap.put( "list" , Person. class );
|
06 |
classMap.put( "map" , Map. class );
|
08 |
MyBeanWithPerson diyBean =(MyBeanWithPerson)JSONObject.toBean(JSONObject.fromObject(json), MyBeanWithPerson. class , classMap);
|
09 |
System.out.println(diyBean); |
11 |
System.out.println( "do the list release" );
|
12 |
List<Person> list = diyBean.getList();
|
13 |
for (Person o : list) {
|
14 |
Person p = (Person) o;
|
15 |
System.out.println(p.getName()); |
18 |
System.out.println( "do the map release" );
|
20 |
MorpherRegistry morpherRegistry = JSONUtils.getMorpherRegistry();
|
21 |
Morpher dynaMorpher = new BeanMorpher(Person. class , morpherRegistry);
|
22 |
morpherRegistry.registerMorpher(dynaMorpher);
|
24 |
Map map = diyBean.getMap(); |
26 |
System.out.println(map); |
27 |
?List<Person> output = new ArrayList();
|
28 |
for (Iterator i = map.values().iterator(); i.hasNext();) {
|
30 |
output.add((Person) morpherRegistry.morph(Person. class , i.next()));
|
33 |
for (Person p : output) {
|
34 |
System.out.println(p.getName()); |
相关推荐
在web项目中经常会用到json数据(如...json-lib-2.4-jdk15.jar json所依赖的jar如下: commons-beanutils-1.8.0.jar commons-collections-3.2.1.jar commons-lang-3.7.jar commons-logging-1.1.1.jar ezmorph-1.0.6.jar
java中封装json数据的包,net.sft.json包,JAVA中使用JSON进行数据传递,用于java生成json字符串,和java解析json字符串。
json-lib是java工程中常用到的json解析工具,使用简单灵活,本压缩包中包括的文件有(使用json-lib必备的jar包): commons-beanutils-1.8.3.jar commons-collections-3.2.1.jar commons-lang-2.6.jar commons-...
commons-beanutils-1.8.0.jar commons-beanutils-...json-lib-2.2.2-jdk15.jar json-lib-2.4-jdk15.jar json-lib-2.4-jdk15-javadoc.jar json-lib-2.4-jdk15-sources.jar morph-1.1.1.jar morph-sandbox-1.1.1.jar
公司项目开发所用的包文件 json-lib: commons-beanutils.jar commons-collections.jar commons-lang-2.0.jar commons-logging.jar ezmorph-1.0.6.jar json-lib-2.3-jdk15.jar org.json: org.json.jar
net.sf.json jar包含以下: commons-lang.jar commons-beanutils.jar commons-collections.jar commons-logging.jar ezmorph.jar json-lib-2.2.2-jdk15.jar
json相关jar包(不使用springmvc开发时)。其中包含(commons-beanutils.jar、commons-collections-3.1.jar、commons-lang-2.6.jar、commons-logging.jar、ezmorph-1.0.6.jar、json-lib-2.2.3-jdk15.jar)
使用json-lib解析json所需要的jar,包括: commons-beanutils-1.7.0.jar、commons-collections-3.2.jar、 commons-lang-2.4.jar、commons-logging-1.1.jar、 ezmorph-1.0.4.jar、json-lib-2.2.2-jdk15.jar
百度逆地址JSON解析示例(httpclient-4.5.jar+json-lib-2.4-jdk15.jar)
json-lib-2.4-jdk15.jar所需全部JAR包.rar java jsoup解析开彩网api接口json数据实例
java操作json文件的6个包。缺少其一都容易报错。亲测可用,直接导入项目即可 1. commons-beanutils-1.8.0 2. commons-collections-3.2.1 3. commons-lang-2.6 4. commons-logging-1.1.1 ...6. json-lib-2.4-jdk15
json-lib-2.2-jdk15.jar morph-1.1.1.jar ezmorph-1.0.5.jar commons-logging-1.2.jar commons-lang-2.6.jar commons-httpclient.jar commons-collections-3.2.1.jar commons-beanutils.jar 我补齐后,该资源包...
编译好的 windows 版的 json-c-0.8 库。压缩包里面包含三个文件夹..\include,..\lib,..\src,”lib\“下有.lib和.dll。要使用该库,需要包含 json.h, json_object.h, json_tokener.h.
lib是一个java类库,它用于把beans, maps, collections, java arrays and XML 传递给一个Json,或者返回来把Json来传递beans, maps, collections, java arrays and XML ,说白了就是用于处理JSON数据的,包括生成和...
微信开发用到的jar包:json 解析(json-lib-2.4-jdk15.jar),dom4j-1.6.1.jar,xstream-1.3.1.jar
json-lib-2.2.2-jdk15.jar压缩包。json解析使用,可以再android和JAVA中使用
json-lib解析所需要的所有jar包。包括commons-beanutils-1.8.0.jar、commons-collections-3.2.1.jar、commons-lang-2.4-1.jar、commons-logging-1.1.1.jar、ezmorph-1.0.6-sources.jar、json-lib-2.4-jdk15.jar
•什么是 Json JSON(JvaScript Object Notation)(官网网站:http://www.json.org/)是 一种轻量级的数据交换格式。 易于人阅读和编写。同时也易于机器解析和生成。它基于 JavaScript ...•json-lib-2.4-jdk15.jar
json-lib 提供了强大的JSON支持,利用Ajax提交上来的JSON字符串进行解析,可以转化为POJO对象,可以从POJO转化为js可以识别的JSON对象。方便易用!
包含:commons-beanutils-1.8.3.jar ,json-lib-2.4-jdk15.jar ,ezmorph-1.0.6.jar,commons-collections-3.2.jar,commons-lang-2.5.jar,commons-logging-1.2.jar