fastjson的基本使用方法

转载自:https://blog.csdn.net/codershamo/article/details/51994720

fastjson主要的API

fastjson入口类是com.alibaba.fastjson.JSON, 主要API是JSON.toJSONStringparseObject,使用fastjson要注意要转换的类必须有默认的无参构造方法。

  • 序列化: 
    String jsonString = JSON.toJSONString(obj);

  • 反序列化: 
    VO vo = JSON.parseObject("jsonString", VO.class);

  • 泛型反序列化:

List<VO> list = JSON.parseObject("jsonString", new TypeReference<List<VO>>(){});

注意:VO必须有默认无参构造方法
  • 1
  • 2
  • 3
  • 其他主要API:
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。
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

说明: 
SerializeWriter:相当于StringBuffer 
JSONArray:相当于List 
JSONObject:相当于Map

JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd";  
JSON.toJSONString(obj, SerializerFeature.WriteDateUseDateFormat);
  • 1
  • 2
  • 反序列化能够自动识别如下日期格式:

    • ISO-8601日期格式
    • yyyy-MM-dd
    • yyyy-MM-dd HH:mm:ss
    • yyyy-MM-dd HH:mm:ss.SSS
    • 毫秒数字
    • 毫秒数字字符串
    • NET JSON日期格式
    • new Date(198293238)

过滤属性

需要根据不同的环境返回定制化返回属性时,可以使用SimplePropertyPreFilter

  • SimplePropertyPreFilter的代码接口如下:
public class SimplePropertyPreFilter implements PropertyPreFilter {      
      public SimplePropertyPreFilter(String... properties){
          this(null, properties);
      }

      public SimplePropertyPreFilter(Class<?> clazz, String... properties){
          ... ...
      }

      public Class<?> getClazz() {
          return clazz;
      }

      public Set<String> getIncludes();

      public Set<String> getExcludes();
  ...
  }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

你可以配置includes、excludes。当class不为null时,针对特定类型;当class为null时,针对所有类型。 
当includes的size > 0时,属性必须在includes中才会被序列化,excludes优先于includes。

  • 使用方法: 
    在1.1.23版本之后,JSON提供新的序列化接口toJSONString,如下:

String JSON.toJSONString(Object, SerializeFilter, SerializerFeature...);

用法如下:

 User user = new User();
 user.setName("shamo");
 user.setPwd("123");
 user.setAge(25);

 SimplePropertyPreFilter filter = new SimplePropertyPreFilter(User.class, "pwd", "age");
 Set<String> includes = filter.getIncludes();
 Set<String> excludes = filter.getExcludes();
 includes.add("name");
 excludes.add("pwd");
 String json = JSON.toJSONString(user, filter);
 System.out.println(json);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

输出:


{"age":25,"name":"shamo"} 

修改属性

可以使用JSONObject对json形式的字符串进行任意的增删改 
用法如下:

String jsonString = JSON.toJSONString(user);
JSONObject jsonObject = JSON.parseObject(jsonString);
jsonObject.put("gender", "male");
jsonObject.put("pwd","456");
jsonObject.remove("name");
String json = JSON.toJSONString(jsonObject);
System.out.println(json);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

输出:

{"pwd":"456","age":25,"gender":"male"}
  • 1

SerializerFeature序列化属性

DisableCheckSpecialChar:一个对象的字符串属性中如果有特殊字符如双引号,将会在转成json时带有反斜杠转移符。如果不需要转义,可以使用这个属性。默认为false 
QuoteFieldNames———-输出key时是否使用双引号,默认为true 
WriteMapNullValue——–是否输出值为null的字段,默认为false 
WriteNullNumberAsZero—-数值字段如果为null,输出为0,而非null 
WriteNullListAsEmpty—–List字段如果为null,输出为[],而非null 
WriteNullStringAsEmpty—字符类型字段如果为null,输出为”“,而非null 
WriteNullBooleanAsFalse–Boolean字段如果为null,输出为false,而非null

WriteClassName:
支持序列化时写入类型信息,从而使得反序列化时不丢失类型信息。例如:
Color color = Color.RED; String text = JSON.toJSONString(color,SerializerFeature.WriteClassName); System.out.println(text); 
输出结果:
{"@type":"java.awt.Color","r":255,"g":0,"b":0,"alpha":255}
由于序列化带了类型信息,使得反序列化时能够自动进行类型识别,例如:
String text = ...; //{"@type":"java.awt.Color","r":255,"g":0,"b":0,"alpha":255}
Color color = (Color) JSON.parse(text);

猜你喜欢

转载自blog.csdn.net/qq_33223299/article/details/80090823