JSON解析工具-json-lib使用教程
一、簡介
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項目當中,即可調用。
三、講解
在進行下面的代碼演示之前,我們先將幾個基本的類介紹一下:
MyBean類:
public class MyBean {
private String id = null;
private String name = null;
private Date date = null;
private List cardNum = null;
private String[] cardType = {"身份證", "銀行卡" , "公車卡"};
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public List getCardNum() {
return cardNum;
}
public void setCardNum(List cardNum) {
this.cardNum = cardNum;
}
public String[] getCardType() {
return cardType;
}
public void setCardType(String[] cardType) {
this.cardType = cardType;
}
}
Person類:
public class Person {
private String name = null;
public Person(){
}
public Person(String name){
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
MyBeanWithPerson類:
public class MyBeanWithPerson {
private List<Person> list = null;
private Map<String,Person> map = null;
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
}
}
1.將json字符串轉換成JSON,根據情況用JSONArray或JSONObject
public static void testJsonStrToJSON() {
JSONArray jsonArray = JSONArray.fromObject("[\"json\",\"is\",\"easy\"]");
System.out.println(jsonArray);
}
2.將Java Bean轉換成JSON對象
public static void testBeadToJSON() {
MyBean bean = new MyBean();
bean.setId("001");
bean.setName("銀行卡");
bean.setDate(new Date());
List cardNum = new ArrayList();
cardNum.add("農行");
cardNum.add("工行");
cardNum.add("建行");
cardNum.add(new Person("test"));
bean.setCardNum(cardNum);
JSONObject jsonObject = JSONObject.fromObject(bean);
System.out.println(jsonObject);
}
3.將一般的數組轉換成JSON
public static void testArrayToJSON() {
boolean[] boolArray = new boolean[] { true, false, true };
JSONArray jsonArray = JSONArray.fromObject(boolArray);
System.out.println(jsonArray);
}
4.將Collection對象轉換成JSON
public static void testListToJSON() { List list = new ArrayList(); list.add("first"); list.add("second"); JSONArray jsonArray = JSONArray.fromObject(list); System.out.println(jsonArray); }
5.將Map轉換成JSON
public static void testMapToJSON() {
Map map = new HashMap();
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 = JSONObject.fromObject(map);
System.out.println(jsonObject);
}
6.將普通類型的JSON字符串轉換成JSON
public static void testJSONToObject() throws Exception { // 將JSon字符串轉換成JsonObject對象 String json = "{name=\"json\",bool:true,int:1,double:2.2,func:\"function(a){ return a; }\",array:[1,2]}"; JSONObject jsonObject = JSONObject.fromObject(json); System.out.println(jsonObject); // 將JsonObject對象轉換成JavaBean對象 Object bean = JSONObject.toBean(jsonObject); System.out.println(PropertyUtils.getProperty(bean, "name")); System.out.println(PropertyUtils.getProperty(bean, "bool")); System.out.println(PropertyUtils.getProperty(bean, "int")); System.out.println(PropertyUtils.getProperty(bean, "double")); System.out.println(PropertyUtils.getProperty(bean, "func")); System.out.println(PropertyUtils.getProperty(bean, "array")); List arrayList = (List) JSONArray.toCollection(jsonObject.getJSONArray("array")); for (Object object : arrayList) { System.out.println(object); } }
7.將復合類型的JSON字符串轉換成復合對象,包含List
public static void testJSONToBeanHavaList() {
String json = "{list:[{name:\"test1\"},{name:\"test2\"}]}";
Map classMap = new HashMap();
classMap.put("list", Person.class);
MyBeanWithPerson diyBean = (MyBeanWithPerson) JSONObject.toBean(
JSONObject.fromObject(json), MyBeanWithPerson.class, classMap);
System.out.println(diyBean);
List list = diyBean.getList();
for (Object o : list) {
if (o instanceof Person) {
Person p = (Person) o;
System.out.println(p.getName());
}
}
}
8.將復合類型的JSON字符串轉換成復合對象,包含Map
public static void testJSONToBeanHavaMap() {
// 把Map看成一個對象
String json = "{list:[{name:\"test1\"},{name:\"test2\"}],map:{test1:{name:\"test1\"},test2:{name:\"test2\"}}}";
Map classMap = new HashMap();
classMap.put("list", Person.class);
classMap.put("map", Map.class);
// 使用暗示,直接將json解析為指定自定義對象,其中List完全解析,Map沒有完全解析
MyBeanWithPerson diyBean =(MyBeanWithPerson)JSONObject.toBean(JSONObject.fromObject(json), MyBeanWithPerson.class, classMap);
System.out.println(diyBean);
System.out.println("do the list release");
List<Person> list = diyBean.getList();
for (Person o : list) {
Person p = (Person) o;
System.out.println(p.getName());
}
System.out.println("do the map release");
// 先往注冊器中注冊變換器,需要用到ezmorph包中的類
MorpherRegistry morpherRegistry = JSONUtils.getMorpherRegistry();
Morpher dynaMorpher = new BeanMorpher(Person.class, morpherRegistry);
morpherRegistry.registerMorpher(dynaMorpher);
Map map = diyBean.getMap();
//這里的map沒進行類型暗示,故按默認的,里面存的為net.sf.ezmorph.bean.MorphDynaBean類型的對象
System.out.println(map);
?List<Person> output = new ArrayList();
for (Iterator i = map.values().iterator(); i.hasNext();) {
// 使用注冊器對指定DynaBean進行對象變換
output.add((Person) morpherRegistry.morph(Person.class, i.next()));
}
for (Person p : output) {
System.out.println(p.getName());
}
}
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!