Json   发布时间:2022-04-22  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了FastJson的使用大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

介绍

FastJson据说是目前为止最快的JSON库,好吧,他说是就是了。

FastJson 的Wiki@L_262_0@:https://github.com/alibaba/fastjson/wiki/

Quick_Start @L_262_0@:https://github.com/alibaba/fastjson/wiki/Quit_Start_cn

FastJson的API非常简单:

String text = JSON.toJSONString(obj); //序列化
VO vo = JSON.parSEObject("{...}",VO.class); //反序列化

代码

接着,上菜,不,上代码
这里需要注意@L_696_7@核心思想:JSON中的键值对就是Java中的Map,JSON中的数组就是Java中的List

POJO对象

package com.owen.fastjson.bean;

/** * POJO对象 * * @author Owen */
public class UserInfo {

    private String name;

    private int age;

    /** * FastJSON要求:需要提供认构造方法 */
    public UserInfo() {}

    public UserInfo(String name,int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

}

测试类

@H_450_125@package com.owen.fastjson.app; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.TypeReference; import com.alibaba.fastjson.serializer.serializerFeature; import com.owen.fastjson.bean.UserInfo; /** * 测试类 * * @author Owen */ public class @H_660_149@main { /** * 简单序列化 */ private static void simpletest() { UserInfo userInfo = new UserInfo("owen",24); String jsonStr = JSON.toJSONString(userInfo); System.out.println(jsonStr); } /** * 复杂序列化 */ public static void complextest() { @H_899_11@map<String,Object> map = new HashMap<String,Object>(); map.put("username","owen"); map.put("age",25); map.put("sex","男"); @H_899_11@map<String,Object> temp = new HashMap<String,Object>(); temp.put("name","jack"); temp.put("age",18); map.put("girinfo",temp); List<String> list = new ArrayList<String>(); list.add("爬山"); list.add("电影"); list.add("旅游"); map.put("hobby",list); String jsonStr = JSON.toJSONString(map); System.out.println(jsonStr); } /** * 日期序列化 */ public static void Datetest() { Date date = new Date(); // 输出 System.out.println("时间戳=" + JSON.toJSONString(datE)); // 认的日期格式 System.out.println("当前日期=" + JSON.toJSONString(date,serializerFeature.WriteDateUseDateFormat)); // 使用指定的日期格式 System.out.println("当前日期=" + JSON.toJSONStringWithDateFormat(date,"yyyy-MM-dd HH:mm:ss",serializerFeature.WriteDateUseDateFormat)); } /** * 简单反序列化 */ public static void simpleDeserializetest() { String userInfoJSONStr = "{\"name\":\"owen\",\"age\":24}"; UserInfo userInfo = JSON.parSEObject(userInfoJSONStr,UserInfo.class); System.out.println("name = " + userInfo.getName() + ",age = " + userInfo.getAge()); } /** * 泛型反序列化 */ public static void genericTypeDeserializetest() { String jsonStr = "{\"user\":{\"name\":\"owen\",\"age\":24}"; @H_899_11@map<String,UserInfo> map = JSON.parSEObject(jsonStr,new TypeReference<@H_899_11@map<String,UserInfo>>() {}); System.out.println(map.get("user").getAge() + "\n"); String jsonStr2 = "{\"user\":[{\"name\":\"owen\",\"age\":24},{\"name\":\"jack\",\"age\":18}]"; @H_899_11@map<String,List<UserInfo>> users = JSON.parSEObject(jsonStr2,List<UserInfo>>>() {}); for (UserInfo info : users.get("user")) { System.out.println("name=" + info.getName()); System.out.println("age=" + info.getAge()); System.out.println("------------------"); } } /** * test */ public static void main(String[] args) { genericTypeDeserializetest(); } }

import com.alibaba.fastjson.serializer.SimpleDateFormatserializer;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;

public class TestFastjson {
//fastjson序列化单个对象 与反序列化
@Test
public void test1() {
employee e = new employee("001","张三",23,new Date());
//序列化
String jsonStr = JSON.toJSONString(E);
System.out.println(jsonStr);
//反序列化
employee emp = JSON.parSEObject(jsonStr,employee.class);
System.out.println(emp.getName());
}
//fastjson序列化list集合 与反序列化
public void test2() {
employee e2 = new employee("002","李四",29,198)">List<employee> emps = new ArrayList<employee>();
emps.add(E);
emps.add(e2);
//fastjson序列化list, 返回来的是@L_696_7@json数组,由[]包含两个json
String jsonArryStr = JSON.toJSONString(emps);
System.out.println(jsonArryStr);
// //反序列化
//法一
// List<employee> empList = JSON.parSEObject(jsonArryStr,new TypeReference<List<employee>>(){} );
//法二
List<employee> empList = JSON.parseArray(jsonArryStr,198)">for (employee employee : empList) {
System.out.println(employee.getName());
System.out.println(employee.getBirthDay());
//fastjson序列化复杂对象 与反序列化
public void test3() {
DEPT dept = new Dept("d001","研发部",emps);
String jsonStr = JSON.toJSONString(dept);
DEPT d = JSON.parSEObject(jsonStr,Dept.class);
System.out.println(d.getName());
//json转map@H_355_333@map<String,Object> map1 = JSON.parSEObject(jsonStr);//返回JSONObject,JSONObject实现Map<String,Object>接口
// Map<String,Object> map1 = (Map<String,Object>)JSON.parse(jsonStr);
for (String key : map1.keySet()) {
System.out.println(key + ":" + map1.get(key));
//fastjson 的 JSONObject的使用
public void test4() {
//反序列化 (可以和test1比较)
JSONObject emp = JSON.parSEObject(jsonStr,JSONObject.class);
System.out.println(emp);
System.out.println(emp.getString("name"));
//再放@L_696_7@employee不存在的字段
emp.put("salary","8000");
System.out.println(emp.toJSONString());
System.out.println(emp.get("salary"));
//fastjson序列化字符串
public void test5(){
List<String> strs = new ArrayList<String>();
strs.add("Hello");
strs.add("world");
strs.add("banana");
String jsonStr = JSON.toJSONString(strs);
List<String> strList = JSON.parSEObject(jsonStr,new TypeReference<List<String>>(){} );
// List<String> strList = JSON.parseArray(jsonStr,String.class);//等同于上一句
for (String str : strList) {
System.out.println(str);
//fastjson过滤字段
public void test6() {
//构造过滤器
SimplePropertyPreFilter filter = new SimplePropertyPreFilter(employee.class,"id","age");
String jsonStr =JSON.toJSONString(emps,filter);
//fastjson 日期处理
public void test7(){
Date date = new Date();
String dateStr = JSON.toJSONString(datE);
System.out.println(dateStr);
String dateStr2 = JSON.toJSONStringWithDateFormat(date,"yyyy-MM-dd HH:mm:ss");
System.out.println(dateStr2);
//序列化实体
employee emp = new employee("001",198)">String empStr = JSON.toJSONStringWithDateFormat(emp,198)">System.out.println(empStr);
String empStr2 = JSON.toJSONString(emp,serializerFeature.WriteDateUseDateFormat);
System.out.println(empStr2);
//法三
serializeConfig config = new serializeConfig();
config.put(Date.class,new SimpleDateFormatserializer("yyyy年MM月dd日 HH时mm分ss秒"));
String empStr3 = JSON.toJSONString(emp,config);
System.out.println(empStr3);
//fastjson 去掉值的双引号 实现JSONAware接口
public void test8(){
//见同级目录的Function.java
//fastjson 注解形式 (别名命名,过滤字段,日期格式)
public void test9(){
student stu = new student("001",198)">String jsonStr = JSON.toJSONString(stu);
}


文/程序猴(简书作者) 原文链接http://www.jianshu.com/p/a04c428963a2 著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。

大佬总结

以上是大佬教程为你收集整理的FastJson的使用全部内容,希望文章能够帮你解决FastJson的使用所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。