zoukankan      html  css  js  c++  java
  • FastJson的用法

    一、简单数据的序列化

    复制代码
    pubic class UserInfo implements Serializable{
     private String name;
     private int age;
     public void setName(String name){
      this.name=name;
     }
     public String getName(){
      return name;
     }
     
     public void setAge(int age){
      this.age=age;
     }
     public int getAge(){
      return age;
     }
    } 
    
    public class TestOne{
     
     public static void main(String[] args){
      UserInfo info=new UserInfo();
      info.setName("zhangsan");
      info.setAge(24);
      //将对象转换为JSON字符串
      String strJson=JSON.toJSONString(info);
      System.out.println("JSON="+strJson);
     }
    
    }
    复制代码

    说明:

    1. 关键部分就是上述部分的JSON.toJSONString();
    2. JSON序列化,默认序列化出的JSON字符串中键值对是使用双引号,如果需要单引号的JSON字符串,[eg:String jsonString = JSON.toJSONString(map,   SerializerFeature.UseSingleQuotes);] fastjson序列化时可以选择的SerializerFeature有十几个属性,你可以按照自己的需要去选择使用。  

    二、反序列化

    复制代码
    public void test3(){
    
     String json="{"name":"chenggang","age":24}";
     //反序列化
     UserInfo userInfo=JSON.parseObject(json,UserInfo.class);
     System.out.println("name:"+userInfo.getName()+", age:"+userInfo.getAge());
     
    }
    /**泛型的反序列化*/
    public static void test4(){
      String json="{"user":{"name":"zhangsan","age":25}}";
      Map<String, UserInfoBean> map = JSON.parseObject(json, new TypeReference<Map<String, UserInfoBean>>(){});
      System.out.println(map.get("user"));
     }
    复制代码

    三、日期格式化

    复制代码
    public void test5(){
    
      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));
    
    }
    复制代码

    说明:

    阿里巴巴提供了很多SerializerFeature.XXX 这些都很符合中国人的习惯。比起Jackson各有优劣!

    四、基本常用API

    复制代码
     Fastjson API入口类是com.alibaba.fastjson.JSON,常用的序列化操作都可以在JSON类上的静态方法直接完成。
    //  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(和上面方法的区别是返回值是不一样的)
    复制代码
  • 相关阅读:
    (2).net体系
    (1)php开篇常识
    java基础知识-xx
    java基础知识-字符串与数组
    java基础知识-流程控制
    小明的喷漆计划
    加分二叉树
    括号序列
    P1045
    胖男孩
  • 原文地址:https://www.cnblogs.com/tiancai/p/6035859.html
Copyright © 2011-2022 走看看