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包有:
JAR | 网址 |
jakarta commons-collections 3.2.1 | http://commons.apache.org/collections/download_collections.cgi |
jakarta commons-logging 1.1.1 | |
ezmorph 1.0.6 |
注意这里如果jakarta commons-lang 使用3.1版本的会报:Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang.exception.NestableRuntimeException
所以这里还是使用2.6的吧
MyBean.java:
package com.iflytek.json;
import net.sf.json.JSONFunction;
/**
* @author xudongwang 2012-1-15
*
* Email:xdwangiflytek@gmail.com
*/
public class MyBean {
private String name = "json";
private int pojoId = 1;
private char[] options = new char[] { 'a', 'f' };
private String func1 = "function(i){ return this.options[i]; }";
private JSONFunction func2 = new JSONFunction(new String[] { "i" },
"return this.options[i];");
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPojoId() {
return pojoId;
}
public void setPojoId(int pojoId) {
this.pojoId = pojoId;
}
public char[] getOptions() {
return options;
}
public void setOptions(char[] options) {
this.options = options;
}
public String getFunc1() {
return func1;
}
public void setFunc1(String func1) {
this.func1 = func1;
}
public JSONFunction getFunc2() {
return func2;
}
public void setFunc2(JSONFunction func2) {
this.func2 = func2;
}
}
JsonlibTest.java:
package com.iflytek.json;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.beanutils.PropertyUtils;
/**
* @author xudongwang 2012-1-15
*
* Email:xdwangiflytek@gmail.com
*/
public class JsonlibTest {
// JSONArray是将一个Java对象转换成json的Array格式,如['xdwang', 22]
private JSONArray jsonArray = null;
// JSONObject是将Java对象转换成一个json的Object形式,如{name:'xdwang', age: 22}
private JSONObject jsonObject = null;
public static void main(String[] args) {
JsonlibTest json = new JsonlibTest();
json.ArrayToJSON();
json.ListToJSON();
json.MapsToJSON();
json.BeanToJSON();
json.JSONToBean();
}
/**
* 数组转JSON操作
*/
public void ArrayToJSON() {
boolean[] boolArray = new boolean[] { true, false, true };
jsonArray = JSONArray.fromObject(boolArray);
System.out.println("数组转JSON操作:" + jsonArray);
}
/**
* 集合转JSON操作
*/
public void ListToJSON() {
List<String> list = new ArrayList<String>();
list.add("first");
list.add("second");
jsonArray = JSONArray.fromObject(list);
System.out.println("集合转JSON操作:" + jsonArray);
}
/**
* Maps转JSON操作
*/
public void MapsToJSON() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", "json");
map.put("bool", Boolean.TRUE);
map.put("int", new Integer(1));
map.put("arr", new String[] { "a", "b" });
map.put("func", "function(i){ return this.arr[i]; }");
jsonObject = JSONObject.fromObject(map);
System.out.println("Maps转JSON操作:" + jsonObject);
}
/**
* Bean转JSON操作
*/
public void BeanToJSON() {
jsonObject = JSONObject.fromObject(new MyBean());
System.out.println("Bean转JSON操作:" + jsonObject);
}
/**
* JSON转Bean操作
*/
public void JSONToBean() {
try {
String json = "{\"func1\":function(i){ return this.options[i]; },\"func2\":function(i){ return this.options[i]; },\"name\":\"json\",\"options\":[\"a\",\"f\"],\"pojoId\":1}";
jsonObject = JSONObject.fromObject(json);
Object bean = JSONObject.toBean(jsonObject);
System.out.println("jsonStr:" + json);
System.out.println("name:" + jsonObject.get("name"));
System.out.println("name:"
+ PropertyUtils.getProperty(bean, "name"));
System.out.println("pojoId:" + jsonObject.get("pojoId"));
System.out.println("pojoId:"
+ PropertyUtils.getProperty(bean, "pojoId"));
System.out.println("options:" + jsonObject.get("options"));
System.out.println("options:"
+ PropertyUtils.getProperty(bean, "options"));
System.out.println("func1:" + jsonObject.get("func1"));
System.out.println("func1:"
+ PropertyUtils.getProperty(bean, "func1"));
System.out.println("func2:" + jsonObject.get("func2"));
System.out.println("func2:"
+ PropertyUtils.getProperty(bean, "func2"));
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
}
这里就不描述xml的相关操作了,具体可以查看文档。