加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Fastjson简单使用方法

发布时间:2020-12-16 18:56:57 所属栏目:百科 来源:网络整理
导读:一、简单数据的序列化 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=

一、简单数据的序列化

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:StringjsonString=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(和上面方法的区别是返回值是不一样的)

JackSon下载:http://repo1.maven.org/maven2/com/alibaba/fastjson/ 与 https://yunpan.cn/cu2iEKCZjXfak 访问密码 8291

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读