Fastjson API 중국어 버전
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
파싱 트리
import com.alibaba.fastjson.*; JSONObject jsonObj = JSON.parseObject(jsonStr);
POJO 구문 분석
import com.alibaba.fastjson.JSON; Model model = JSON.parseObject(jsonStr, Model.class);
POJO 일반 구문 분석
import com.alibaba.fastjson.JSON; Type type = new TypeReference<List<Model>>() {}.getType(); List<Model> list = JSON.parseObject(jsonStr, type);
POJO를 json 문자열로 변환
import com.alibaba.fastjson.JSON;Model model = ...; String jsonStr = JSON.toJSONString(model);
POJO를 json 바이트로 변환
import com.alibaba.fastjson.JSON;Model model = ...; byte[] jsonBytes = JSON.toJSONBytes(model);
POJO를 json 문자열로 OutputStream
import com.alibaba.fastjson.JSON;Model model = ...; OutputStream os;JSON.writeJSONString(os, model);
Writer
import com.alibaba.fastjson.JSON; Model model = ...; Writer writer = ...; JSON.writeJSONString(writer, model);