| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
jopen
11年前发布

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 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
 转载本站原创文章,请注明出处,并保留原始链接、图片水印。
 本站是一个以用户分享为主的开源技术平台,欢迎各类分享!
 本文地址:https://www.open-open.com/lib/view/open1381566795364.html
json-lib JSON开发包