讲师中心 微信公众号

工具推荐

视频效率加速
Laper
Laper

剧本策划到成片协作

AniShort
AniShort

AI短剧协同制作平台

立刻MV
立刻MV

低成本歌曲 MV、视频生成

剧云
剧云

中文短剧剧本制作平台

FastJson教程手册

浏览17277
更新时间2022-04-19

Fastjson API中文版

JSON这个类是fastjson API的入口,主要的功能都通过这个类提供。

序列化API

package com.alibaba.fastjson;public abstract class JSON {    // 将Java对象序列化为JSON字符串,支持各种各种Java基本类型和JavaBean
    public static String toJSONString(Object object, SerializerFeature... features);    // 将Java对象序列化为JSON字符串,返回JSON字符串的utf-8 bytes
    public static byte[] toJSONBytes(Object object, SerializerFeature... features);    // 将Java对象序列化为JSON字符串,写入到Writer中
    public static void writeJSONString(Writer writer, 
                                       Object object, 
                                       SerializerFeature... features);    // 将Java对象序列化为JSON字符串,按UTF-8编码写入到OutputStream中
    public static final int writeJSONString(OutputStream os, // 
                                            Object object, // 
                                            SerializerFeature... features);



}

JSON字符串反序列化API

package com.alibaba.fastjson;

public abstract class JSON {
    // 将JSON字符串反序列化为JavaBean
    public static <T> T parseObject(String jsonStr, 
                                    Class<T> clazz, 
                                    Feature... features);

    // 将JSON字符串反序列化为JavaBean
    public static <T> T parseObject(byte[] jsonBytes,  // UTF-8格式的JSON字符串
                                    Class<T> clazz, 
                                    Feature... features);

    // 将JSON字符串反序列化为泛型类型的JavaBean
    public static <T> T parseObject(String text, 
                                    TypeReference<T> type, 
                                    Feature... features);

    // 将JSON字符串反序列为JSONObject
    public static JSONObject parseObject(String text);
}

Demo

parse Tree

import com.alibaba.fastjson.*;

JSONObject jsonObj = JSON.parseObject(jsonStr);

parse POJO

import com.alibaba.fastjson.JSON;

Model model = JSON.parseObject(jsonStr, Model.class);

parse POJO Generic

import com.alibaba.fastjson.JSON;

Type type = new TypeReference<List<Model>>() {}.getType(); 
List<Model> list = JSON.parseObject(jsonStr, type);

convert POJO to json string

import com.alibaba.fastjson.JSON;Model model = ...; 
String jsonStr = JSON.toJSONString(model);

convert POJO to json bytes

import com.alibaba.fastjson.JSON;Model model = ...; 
byte[] jsonBytes = JSON.toJSONBytes(model);

write POJO as json string to OutputStream

import com.alibaba.fastjson.JSON;Model model = ...; 
OutputStream os;JSON.writeJSONString(os, model);

write POJO as json string to Writer

import com.alibaba.fastjson.JSON;

Model model = ...; 
Writer writer = ...;
JSON.writeJSONString(writer, model);

精品课程

更多
前端入门_HTML5
前端入门_HTML5

共29课时 | 65.3万人学习

CSS视频教程-玉女心经版
CSS视频教程-玉女心经版

共25课时 | 41.5万人学习

JavaScript极速入门_玉女心经系列
JavaScript极速入门_玉女心经系列

共43课时 | 77.2万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn