一、简介
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);
- } <span style="font-family: Simsun; font-size: 15.555556297302246px;"> </span>
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);
- }
- } <span style="font-family: Simsun; font-size: 15.555556297302246px;"> </span>
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());
- }
- }