lazzyRabbit hello

1、实体类与JSON对象的互相转化

        //实体类转JSON
        ChartData chartData = new ChartData();
        chartData.setName("直接访问");
        chartData.setValue(335);
        //1、使用JSONObject
        JSONObject json = JSONObject.fromObject(chartData);
        String strJson=json.toString();
        //2、使用JSONArray
        JSONArray array=JSONArray.fromObject(chartData);
        String strArray=array.toString();
        //3、使用JSON
        com.alibaba.fastjson.JSONObject jsonObj = (com.alibaba.fastjson.JSONObject) JSON.toJSON(chartData);  
        System.out.println("strChartData1:"+strJson);
        System.out.println("strChartData2:"+strArray);
        System.out.println("strChartData3:"+jsonObj.toString());
        //JSON转实体类
        //1、使用JSONObject
        JSONObject jsonObject=JSONObject.fromObject(strJson);
        ChartData cd=(ChartData)JSONObject.toBean(jsonObject, ChartData.class);       
        //2、使用JSONArray
        JSONArray jsonArray=JSONArray.fromObject(strArray);
        //获得jsonArray的第一个元素
        Object o=jsonArray.get(0);
        JSONObject jsonObject2=JSONObject.fromObject(o);
        ChartData cd2=(ChartData)JSONObject.toBean(jsonObject2, ChartData.class);
        //3、使用JSON
        ChartData cd3=(ChartData) JSON.parseObject(strJson, ChartData.class);
        System.out.println("ChartData:"+cd.toString());
        System.out.println("ChartData2:"+cd2.toString());        
        System.out.println("ChartData3:"+cd3.toString());


2、List与JSON数据的互相转化

        List list = new ArrayList();
        list.add("直接访问");
        list.add("邮件营销");
        list.add("联盟广告");
        list.add("视频广告");
        list.add("搜索引擎");
        //List转JSON
        //1、使用JSONObject
        //报错net.sf.json.JSONException: 'object' is an array. Use JSONArray instead
        //JSONObject listObject=JSONObject.fromObject(list);
        //System.out.println("listObject:"+listObject.toString());
        //2、使用JSONArray
        JSONArray listArray=JSONArray.fromObject(list);
        System.out.println("listArray:"+listArray.toString());
        //3、使用JSON
        com.alibaba.fastjson.JSONArray listJSON=(com.alibaba.fastjson.JSONArray) JSON.toJSON(list);
        System.out.println("listJSON:"+listJSON.toString());
        //JSON转List
        //1、使用JSONArray
        List list2=(List)JSONArray.toCollection(listArray);
        System.out.println("list1:"+list2.toString());
        //2、使用JSON
        List list3=(List)JSON.parse(listArray.toString());
        System.out.println("list2:"+list3.toString());

3、Map与JSON对象的相互转换

        //Map转JSON字符串
        Map map=new HashMap();
        map.put("name", "小明");
        map.put("sex", "女");
        //1、JSONObject
        JSONObject mapObject=JSONObject.fromObject(map);
        System.out.println("mapObject1"+mapObject.toString());
        //2、JSONArray
        JSONArray mapArray=JSONArray.fromObject(map);
        System.out.println("mapArray2:"+mapArray.toString());
        //3、JSON
        com.alibaba.fastjson.JSONObject mapJson=(com.alibaba.fastjson.JSONObject) JSON.toJSON(map);
        System.out.println("mapArray3:"+mapJson.toString());
        //JSON转Map
        //1、JSON com.alibaba.fastjson.JSON
        Map map2 = (Map) JSON.parse(mapObject.toString());
        System.out.println("map:"+map2.toString());

注意:

a.代转化的bean如果自定义了构造方法,必须写上默认的构造方法

b.对于Date和Timestamp日期类型字段需要单独处理

c.json相关包要导全

参考:http://www.cnblogs.com/free-dom/p/5801866.html

          http://www.cnblogs.com/qqzy168/p/3938937.html

          http://www.cnblogs.com/jtlgb/p/6170833.html