zoukankan      html  css  js  c++  java
  • JSON之FastJson

    FastJson是什么?

    从网上查到---

    官网地址:http://code.alibabatech.com/wiki/display/FastJSON/Overview(已关闭)

    FastJSOn是阿里巴巴开源的JSON处理工具,包括“序列化”和“反序列化”两部分,它具备如下特征:

    1. 速度最快,测试表明,fastjson具有极快的性能,超越任其他的java json parser。包括自称最快的jackson。
    2. 功能强大,完全支持java bean、集合、Map、日期、Enum,支持范型,支持自省。
    3. 无依赖,能够直接运行在Java SE 5.0以上版本
    4. 支持Android。
    5. 开源 (Apache 2.0)

    既然速度快,当然要体验一下。更何况是国产货,当然要支持了。测试一下,以后在项目里使用...

     1 package test;
     2 import java.io.Serializable;
     3 /**
     4  * <一句话功能简述>
     5  * @author  aliger Email:liqimo#gmail.com
     6  * @version  [V1.00, 2014-8-16]
     7  * @see  [相关类/方法]
     8  * @since V1.00
     9  */
    10 public class UserInfo implements Serializable{  
    11     private String name;  
    12     private int age;  
    13     public void setName(String name){  
    14      this.name=name;  
    15     }  
    16     public String getName(){  
    17      return name;  
    18     }  
    19     public void setAge(int age){  
    20      this.age=age;  
    21     }  
    22     public int getAge(){  
    23      return age;  
    24     }  
    25 } 

    Main:

     1 package test;
     2 import java.util.ArrayList;
     3 import java.util.Date;
     4 import java.util.HashMap;
     5 import java.util.List;
     6 import java.util.Map;
     7 import com.alibaba.fastjson.JSON;
     8 import com.alibaba.fastjson.TypeReference;
     9 import com.alibaba.fastjson.serializer.SerializerFeature;
    10 /**
    11  * <一句话功能简述>
    12  *  
    13  * @author  aliger Email:liqimo#gmail.com
    14  * @version  [V1.00, 2014-8-16]
    15  * @see  [相关类/方法]
    16  * @since V1.00
    17  */
    18 public class TestOne
    19 { 
    20     public static FastJsonUtils ff;
    21 
    22      //public static void main(String[] args){
    23     public static void test1(){
    24           UserInfo info=new UserInfo(); 
    25           info.setName("zhangsan"); 
    26           info.setAge(24); 
    27           //将对象转换为JSON字符串 
    28           //String str_json=JSON.toJSONString(info); 
    29           String str_json=ff.getJsonString(info);
    30           System.out.println("JSON="+str_json); 
    31      }
    32      public static void test2() {  
    33          
    34          HashMap<String, Object> map = new HashMap<String, Object>();  
    35          map.put("username", "zhangsan");  
    36          map.put("age", 24);  
    37          map.put("sex", "男");  
    38          //map集合  
    39          HashMap<String, Object> temp = new HashMap<String, Object>();  
    40          temp.put("name", "xiaohong");  
    41          temp.put("age", "23");  
    42          map.put("girlInfo", temp);  
    43          //list集合  
    44          List<String> list = new ArrayList<String>();  
    45          list.add("爬山");  
    46          list.add("骑车");  
    47          list.add("旅游");  
    48          map.put("hobby", list);  
    49          /*JSON 序列化,默认序列化出的JSON字符串中键值对是使用双引号,如果需要单引号的JSON字符串, [eg:String jsonString = JSON.toJSONString(map,   SerializerFeature.UseSingleQuotes);] 
    50           *fastjson序列化时可以选择的SerializerFeature有十几个属性,你可以按照自己的需要去选择使用。  
    51           */  
    52          String jsonString = JSON.toJSONString(map);  
    53          System.out.println("JSON===" + jsonString);  
    54        }  
    55      public static void test3(){  
    56          String json="{"name":"chenggang","age":24}";  
    57          //反序列化  
    58          UserInfo userInfo=JSON.parseObject(json,UserInfo.class);  
    59          System.out.println("name:"+userInfo.getName()+", age:"+userInfo.getAge());  
    60          }  
    61         /**泛型的反序列化*/  
    62         public static void test4(){  
    63           String json="{"user":{"name":"zhangsan","age":25}}";  
    64           Map<String, UserInfo> map = JSON.parseObject(json, new TypeReference<Map<String, UserInfo>>(){});  
    65           System.out.println(map.get("user"));
    66          }  
    67      public static void test5(){  
    68          Date date=new Date();    
    69          //输出毫秒值  
    70          System.out.println(JSON.toJSONString(date));  
    71          //默认格式为yyyy-MM-dd HH:mm:ss    
    72          System.out.println(JSON.toJSONString(date, SerializerFeature.WriteDateUseDateFormat));  
    73          //根据自定义格式输出日期   
    74          System.out.println(JSON.toJSONStringWithDateFormat(date, "yyyy-MM-dd", SerializerFeature.WriteDateUseDateFormat));  
    75        }  
    76      public static void main(String[] args) {
    77         test1();
    78         test2();
    79         test3();
    80         test4();
    81         test5();
    82     }
    83 }

    out结果:

    1 JSON={"age":24,"name":"zhangsan"}
    2 JSON==={"age":24,"girlInfo":{"age":"23","name":"xiaohong"},"hobby":["爬山","骑车","旅游"],"sex":"男","username":"zhangsan"}
    3 name:chenggang, age:24
    4 test.UserInfo@30c221
    5 1408190003876
    6 "2014-08-16 19:53:23"
    7 "2014-08-16"
  • 相关阅读:
    VysorPro助手
    Play 2D games on Pixel running Android Nougat (N7.1.2) with Daydream View VR headset
    Play 2D games on Nexus 6P running Android N7.1.1 with Daydream View VR headset
    Native SBS for Android
    ADB和Fastboot最新版的谷歌官方下载链接
    How do I install Daydream on my phone?
    Daydream Controller手柄数据的解析
    蓝牙BLE传输性能及延迟分析
    VR(虚拟现实)开发资源汇总
    Android(Java)控制GPIO的方法及耗时分析
  • 原文地址:https://www.cnblogs.com/aliger/p/3916848.html
Copyright © 2011-2022 走看看