你必须掌握的Java基础:json-lib

发布时间:2016-12-10 11:15:08 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"你必须掌握的Java基础:json-lib",主要涉及到你必须掌握的Java基础:json-lib方面的内容,对于你必须掌握的Java基础: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类:  [java] view plaincopy 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类:  [java] view plaincopy 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  [java] view plaincopy public static void testJsonStrToJSON() {        JSONArray jsonArray = JSONArray.fromObject("[\"json\",\"is\",\"easy\"]");    ᅠ ᅠ System.out.println(jsonArray);    }    2.将Java Bean转换成JSON对象  [java] view plaincopy 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  [java] view plaincopy public static void testArrayToJSON() {        boolean[] boolArray = new boolean[] { true, false, true };        JSONArray jsonArray = JSONArray.fromObject(boolArray);        System.out.println(jsonArray);    }    4.将Collection对象转换成JSON  [java] view plaincopy 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  [java] view plaincopy 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  [java] view plaincopy 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  [java] view plaincopy 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  [java] view plaincopy 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());        }    http://wenwen.51.com/290754361-130.html http://wenwen.51.com/290754361-131.html http://wenwen.51.com/290754361-132.html http://wenwen.51.com/290756642-182.html http://wenwen.51.com/290756642-183.html http://wenwen.51.com/290756854-148.html http://wenwen.51.com/290756854-149.html http://wenwen.51.com/290756854-150.html http://wenwen.51.com/290757013-150.html http://wenwen.51.com/290757013-151.html http://wenwen.51.com/290757013-152.html http://wenwen.51.com/290757159-107.html http://wenwen.51.com/290757159-108.html http://wenwen.51.com/290757159-109.html http://wenwen.51.com/290757401-141.html http://wenwen.51.com/290757401-142.html http://wenwen.51.com/290757401-143.html http://wenwen.51.com/290757589-116.html http://wenwen.51.com/290757589-117.html http://wenwen.51.com/290757589-118.html http://wenwen.51.com/290757755-131.html http://wenwen.51.com/290757755-132.html http://wenwen.51.com/290757755-133.html http://wenwen.51.com/290757824-136.html http://wenwen.51.com/290757824-137.html http://wenwen.51.com/290757970-169.html http://wenwen.51.com/290757970-170.html http://wenwen.51.com/290757970-171.html http://wenwen.51.com/290758155-155.html http://wenwen.51.com/290758155-156.html http://wenwen.51.com/290758155-157.html http://wenwen.51.com/290758658-117.html http://wenwen.51.com/290758658-118.html http://wenwen.51.com/290758658-119.html http://www.joy.cn/space/1600551319_article_showArticleInfo_articleId_140737.htm http://www.joy.cn/space/1600551319_article_showArticleInfo_articleId_140884.htm http://www.joy.cn/space/1600551319_article_showArticleInfo_articleId_141093.htm http://www.joy.cn/space/1600551319_article_showArticleInfo_articleId_141425.htm http://www.joy.cn/space/1600551319_article_showArticleInfo_articleId_141424.htm http://www.joy.cn/space/1600551319_article_showArticleInfo_articleId_141429.htm http://www.joy.cn/space/1600551319_article_showArticleInfo_articleId_141434.htm http://www.joy.cn/space/1600551319_article_showArticleInfo_articleId_141446.htm http://www.joy.cn/space/1600551319_article_showArticleInfo_articleId_141459.htm http://www.joy.cn/space/1600551319_article_showArticleInfo_articleId_141470.htm http://www.joy.cn/space/1600551319_article_showArticleInfo_articleId_141489.htm http://www.joy.cn/space/1600551319_article_showArticleInfo_articleId_141495.htm http://www.joy.cn/space/1600551319_article_showArticleInfo_articleId_141499.htm http://www.joy.cn/space/1600551319_article_showArticleInfo_articleId_141497.htm http://www.joy.cn/space/1600551319_article_showArticleInfo_articleId_141516.htm http://www.joy.cn/space/1600551319_article_showArticleInfo_articleId_141519.htm http://www.joy.cn/space/1600551319_article_showArticleInfo_articleId_141520.htm http://www.joy.cn/space/1600551319_article_showArticleInfo_articleId_141571.htm http://www.joy.cn/space/1600551319_article_showArticleInfo_articleId_141572.htm http://www.joy.cn/space/1600551319_article_showArticleInfo_articleId_141573.htm http://www.joy.cn/space/1600551319_article_showArticleInfo_articleId_141609.htm http://www.joy.cn/space/1600551319_article_showArticleInfo_articleId_141611.htm http://www.joy.cn/space/1600551319_article_showArticleInfo_articleId_141682.htm http://www.joy.cn/space/1600551319_article_showArticleInfo_articleId_141683.htm http://www.joy.cn/space/1600551319_article_showArticleInfo_articleId_141685.htm http://www.joy.cn/space/1600551319_article_showArticleInfo_articleId_141724.htm http://www.joy.cn/space/1600551319_article_showArticleInfo_articleId_141803.htm http://www.joy.cn/space/1600551319_article_showArticleInfo_articleId_141808.htm http://www.joy.cn/space/1600551319_article_showArticleInfo_articleId_141817.htm http://www.joy.cn/space/1600551319_article_showArticleInfo_articleId_141828.htm http://www.joy.cn/space/1600551319_article_showArticleInfo_articleId_141854.htm http://www.joy.cn/space/1600551319_article_showArticleInfo_articleId_141858.htm http://www.joy.cn/space/1600551319_article_showArticleInfo_articleId_141886.htm http://www.joy.cn/space/1600551319_article_showArticleInfo_articleId_141899.htm http://www.joy.cn/space/1600551319_article_showArticleInfo_articleId_141902.htm http://www.joy.cn/space/1600551319_article_showArticleInfo_articleId_141919.htm http://www.joy.cn/space/1600551319_article_showArticleInfo_articleId_141979.htm http://www.joy.cn/space/1600551319_article_showArticleInfo_articleId_141982.htm http://www.joy.cn/space/1600551319_article_showArticleInfo_articleId_141987.htm http://www.joy.cn/space/1600551319_article_showArticleInfo_articleId_142017.htm http://www.joy.cn/space/1600551319_article_showArticleInfo_articleId_142021.htm http://www.joy.cn/space/1600551319_article_showArticleInfo_articleId_142024.htm http://www.joy.cn/space/1600551319_article_showArticleInfo_articleId_142084.htm http://www.joy.cn/space/1600551319_article_showArticleInfo_articleId_142090.htm http://www.joy.cn/space/1600551319_article_showArticleInfo_articleId_142093.htm http://www.joy.cn/space/1600551319_article_showArticleInfo_articleId_142097.htm http://www.joy.cn/space/1600551319_article_showArticleInfo_articleId_142100.htm http://www.joy.cn/space/1600551319_article_showArticleInfo_articleId_142143.htm http://www.joy.cn/space/1600551319_article_showArticleInfo_articleId_142146.htm http://www.joy.cn/space/1600551319_article_showArticleInfo_articleId_142147.htm

上一篇:地球人己阻止不了程序猿们学习cocos2d-x了 (第四篇)
下一篇:java.util.Date 与 java.sql.Date互转 及 字符串转换为日期时间格式

相关文章

相关评论