作者:php.cn 更新时间:2022-04-19 14:33:02
Fastjson API 比较
许可证和项目URL
Maven
fastjson
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.11</version>
</dependency>
fastjson-android
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.51.android</version>
</dependency>
杰克逊
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.7.3</version>
</dependency>
gson
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.6.2</version>
</dependency>
API
Fastjson 解析 Tree
import com.alibaba.fastjson.*;
JSONObject jsonObj = JSON.parseObject(jsonStr);
Fastjson 解析 POJO
import com.alibaba.fastjson.JSON;
Model model = JSON.parseObject(jsonStr, Model.class
Fastjson 解析 POJO 泛型
import com.alibaba.fastjson.JSON;
Type type = new TypeReference<List<Model>>() {}.getType();
List<Model> list = JSON.parseObject(jsonStr, type);
Fastjson 转换POJO 转 json 字符串
import com.alibaba.fastjson.JSON;
Model model = ...;
String jsonStr = JSON.toJSONString(model);
Fastjson 将 POJO 转换为 json 字节
import com.alibaba.fastjson.JSON;
Model model = ...;
byte[] jsonBytes = JSON.toJSONBytes(model)
Fastjson 将 POJO 作为 json 字符串写入 OutputStream
import com.alibaba.fastjson.JSON;
Model model = ...;
OutputStream os;
JSON.writeJSONString(os, model);;
Fastjson 将 POJO 作为 json 字符串写入 Writer
import com.alibaba.fastjson.JSON;
Model model = ...;
Writer writer = ...;
JSON.writeJSONString(writer, model);