Json_lib可以方便的将Java对象转成json格式的字符串,也可以将Java对象转换成xml格式的文档,同样可以将Json字符串转换成Java对象,或者将xml字符串转换成Java对象。
官网:
JSON在线转换:
JSON教程:
官网上说明了json_lib还需要依赖的Jar包有:
JAR
网址
jakarta commons-lang 2.5
jakarta commons-beanutils 1.8.0
jakarta commons-collections 3.2.1
jakarta commons-logging 1.1.1
ezmorph1.0.6
注意这里如果jakarta commons-lang 使用3.1版本的会报:Caused by: java.lang.ClassNotFoundException:org.apache.commons.lang.exception.NestableRuntimeException
所以这里还是使用2.6的吧
MyBean.java:
01packagecom.iflytek.json;02
03
importnet.sf.json.JSONFunction;04
05
/**06
* @author xudongwang 2012-1-1507
*08
* Email:[emailprotected]09
*/10
publicclassMyBean {11
privateString name = "json";12
privateintpojoId = 1;13
privatechar[] options = newchar[] { 'a','f'};14
privateString func1 = "function(i){ return this.options[i]; }";15
privateJSONFunction func2 = newJSONFunction(newString[] { "i"},16
"return this.options[i];");17
18
19
publicString getName() {20
returnname;21
}22
23
publicvoidsetName(String name) {24
this.name = name;25
}26
27
publicintgetPojoId() {28
returnpojoId;29
}30
31
publicvoidsetPojoId(intpojoId) {32
this.pojoId = pojoId;33
}34
35
publicchar[] getOptions() {36
returnoptions;37
}38
39
publicvoidsetOptions(char[] options) {40
this.options = options;41
}42
43
publicString getFunc1() {44
returnfunc1;45
}46
47
publicvoidsetFunc1(String func1) {48
this.func1 = func1;49
}50
51
publicJSONFunction getFunc2() {52
returnfunc2;53
}54
55
publicvoidsetFunc2(JSONFunction func2) {56
this.func2 = func2;57
}58
59
}JsonlibTest.java:001
packagecom.iflytek.json;002
003
importjava.lang.reflect.InvocationTargetException;004
importjava.util.ArrayList;005
importjava.util.HashMap;006
importjava.util.List;007
importjava.util.Map;008
009
importnet.sf.json.JSONArray;010
importnet.sf.json.JSONObject;011
012
importorg.apache.commons.beanutils.PropertyUtils;013
014
/**015
* @author xudongwang 2012-1-15016
*017
* Email:[emailprotected]018
*/019
publicclassJsonlibTest {020
021
// JSONArray是将一个Java对象转换成json的Array格式,如['xdwang', 22]022
privateJSONArray jsonArray = null;023
// JSONObject是将Java对象转换成一个json的Object形式,如{name:'xdwang', age: 22}024
privateJSONObject jsonObject = null;025
026
publicstaticvoidmain(String[] args) {027
JsonlibTest json = newJsonlibTest();028
json.ArrayToJSON();029
json.ListToJSON();030
json.MapsToJSON();031
json.BeanToJSON();032
json.JSONToBean();033
}034
035
/**036
* 数组转JSON操作037
*/038
publicvoidArrayToJSON() {039
boolean[] boolArray = newboolean[] { true,false,true};040
jsonArray = JSONArray.fromObject(boolArray);041
System.out.println("数组转JSON操作:"+ jsonArray);042
}043
044
/**045
* 集合转JSON操作046
*/047
publicvoidListToJSON() {048
List list = newArrayList();049
list.add("first");050
list.add("second");051
jsonArray = JSONArray.fromObject(list);052
System.out.println("集合转JSON操作:"+ jsonArray);053
}054
055
/**056
* Maps转JSON操作057
*/058
publicvoidMapsToJSON() {059
Map map = newHashMap();060
map.put("name","json");061
map.put("bool", Boolean.TRUE);062
map.put("int",newInteger(1));063
map.put("arr",newString[] { "a","b"});064
map.put("func","function(i){ return this.arr[i]; }");065
jsonObject = JSONObject.fromObject(map);066
System.out.println("Maps转JSON操作:"+ jsonObject);067
}068
069
/**070
* Bean转JSON操作071
*/072
publicvoidBeanToJSON() {073
jsonObject = JSONObject.fromObject(newMyBean());074
System.out.println("Bean转JSON操作:"+ jsonObject);075
}076
077
/**078
* JSON转Bean操作079
*/080
publicvoidJSONToBean() {081
try{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);086
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
}