zoukankan      html  css  js  c++  java
  • FastJson的简单实用

    一、FastJson的理解

    在工作中,经常客服端需要和服务端进行通信,目前很多项目都采用JSON的方式进行数据传输,简单的参数可以通过手动拼接JSON字符串,但如果请求的参数过多,采用手动拼接JSON字符串,出错率就非常大了。并且工作效率也特别低。

    常用一些开源的JSON框架,比如Google提供的Gson,Jackson,FastJson等框架。
    FastJson不依赖于第三方包, 直接可以运行在Java JDK1.5之上,FastJson完全支持
    http://json.org的标准,支持各种JDK类型,包括基本类型、JavaBean、Collection、Map、Enum、泛型等
    还支持循环引用。
    FastJson项目是开源的:Fastjson代码托管在github.org上,项目地址是
    https://github.com/AlibabaTech/fastjson

    一个JSON库涉及的最基本功能就是序列化和反序列化。Fastjson支持java bean的直接序列化。使用com.alibaba.fastjson.JSON这个类进行序列化和反序列化。

    FastJson是阿里巴巴工程师设计的

    二、FastJson的基本使用

    package cn.monster.entity;
    
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.List;
    
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.serializer.SerializerFeature;
    
    /**
     * FastJson对json数据的处理
     * @author monster
     * @date 2015-08-10
     * 
     *FastJson的常用用法
     *public static final Object parse(String text); // 把JSON文本parse为JSONObject或者JSONArray 
     *public static final JSONObject parseObject(String text); // 把JSON文本parse成JSONObject    
     *------>>>public static final <T> T parseObject(String text, Class<T> clazz); // 把JSON文本parse为JavaBean 
     *public static final JSONArray parseArray(String text); // 把JSON文本parse成JSONArray 
     *public static final <T> List<T> parseArray(String text, Class<T> clazz); //把JSON文本parse成JavaBean集合 
     *---->>>>public static final String toJSONString(Object object); // 将JavaBean序列化为JSON文本  
     *---->>>>public static final String toJSONString(Object object, boolean prettyFormat); // 将JavaBean序列化为带格式的JSON文本 
     *---->>>>public static final Object toJSON(Object javaObject); //将JavaBean转换为JSONObject或者JSONArray。
     */
    public class FastJson {
        public static void main(String[] args) {
                User user=new User();
                user.setName("monster");
                user.setAge(21);
                String json=JSON.toJSONString(user); //将数据解析成json数据
                System.out.println("JSON :" +json);
              
                User Asyuser=JSON.parseObject(json, User.class);
                System.out.println("User  "+Asyuser.getName());
            
            //数据--->json数据
            HashMap<String, Object> map=new HashMap<String, Object>();
            map.put("username"," monster");
            map.put("age", 24);
            map.put("sex","boy");
            
            HashMap<String,Object> temp=new HashMap<String,Object>();
            temp.put("name", "YY");
            temp.put("age", 21);
            temp.put("sex","girl");
            
            map.put("girlInfo", temp);  //--->把temp添加到map中,因为map这个对象的值的数据类型为Object
            
            List<String> list=new ArrayList<String>();
            list.add("爬山");
            list.add("骑车");
            list.add("旅游");
            map.put("hobby", list);
             
            //JSON 序列化,默认序列化出的JSON字符串中键值对是使用双引号,如果需要单引号的JSON字符串
            String jsonString = JSON.toJSONString(map);   //双引号
            //String jsonString = JSON.toJSONString(map,   SerializerFeature.UseSingleQuotes); //-->单引号
            System.out.println("JSON=" + jsonString);  
        
            //输出结果如下
            /**
              * JSON={
                    "sex": "boy",
                    "username": " monster",
                    "age": 24,
                    "hobby": [
                        "爬山",
                        "骑车",
                        "旅游"
                    ],
                    "girlInfo": {
                        "sex": "girl",
                        "age": 21,
                        "name": "YY"
                    }
                }
             */
            
            
            /**
             * json数据解析
              */
            String newJsonString="{"name":"chenggang","age":24}"; 
             //反序列化 
             User userInfo=JSON.parseObject(newJsonString,User.class); 
             System.out.println("name:"+userInfo.getName()+", age:"+userInfo.getAge());  
             
             /**
               * 日期格式化
                 */
              Date date=new Date();   
              //输出毫秒值 
              System.out.println(JSON.toJSONString(date)); 
              //默认格式为yyyy-MM-dd HH:mm:ss   
              System.out.println(JSON.toJSONString(date, SerializerFeature.WriteDateUseDateFormat)); 
              //根据自定义格式输出日期  
              System.out.println(JSON.toJSONStringWithDateFormat(date, "yyyy-MM-dd", SerializerFeature.WriteDateUseDateFormat));
              
              /**
               *   输出结果
                 *   1439164604282
               *    "2015-08-10 07:56:44"
               *    "2015-08-10"
               */
        }
    
    }
    
  • 相关阅读:
    无线鼠标换电池了
    Jython Interactive Servlet Console YOU WILL NEVER KNOW IT EXECLLENT!!! GOOD
    Accessing Jython from Java Without Using jythonc
    jython podcast cool isnt't it?
    Python里pycurl使用记录
    Creating an Interactive JRuby Console for the Eclipse Environment
    微软为AJAX和jQuery类库提供CDN服务
    Download A File Using Cygwin and cURL
    What is JMRI?这个是做什么用的,我真没看懂但看着又很强大
    用curl 发送指定的大cookie的http/https request
  • 原文地址:https://www.cnblogs.com/boy1025/p/4716962.html
Copyright © 2011-2022 走看看