来源:阿里巴巴
链接:https://github.com/Alibaba/fastjson/wiki/%E9%A6%96%E9%A1%B5
Fastjson是一个Java语言编写的高性能功能完善的JSON库。它采用一种“假定有序快速匹配”的算法,把JSON Parse的性能提升到极致,是目前Java语言中最快的JSON库。Fastjson接口简单易用,已经被广泛使用在缓存序列化、协议交互、Web输出、Android客户端等多种应用场景。
Tool | License | URL |
---|---|---|
Fastjson | Apaceh 2.0 | https://github.com/alibaba/fastjson |
Gson | Apaceh 2.0 | https://github.com/google/gson |
Jackson | Apaceh 2.0 | https://github.com/FasterXML/jackson-core |
常见问题解答
https://github.com/alibaba/fastjson/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98
下载
android 开发者请看 这里
或者通过 Maven:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>VERSION_CODE</version>
</dependency>
或者通过 Gradle:
compile 'com.alibaba:fastjson:VERSION_CODE'
用 这里 或者 这里 的真实发行版本号, 替换 VERSION_CODE , 例如 1.2.11
API
Fastjson parse Tree
import com.alibaba.fastjson.*;
JSONObject jsonObj = JSON.parseObject(jsonStr);
Fastjson parse POJO
import com.alibaba.fastjson.JSON;
Model model = JSON.parseObject(jsonStr, Model.class);
Fastjson parse POJO Generic
import com.alibaba.fastjson.JSON;
Type type = new TypeReference<List<Model>>() {}.getType();
List<Model> list = JSON.parseObject(jsonStr, type);
Fastjson convert POJO to json string
import com.alibaba.fastjson.JSON;
Model model = ...;
String jsonStr = JSON.toJSONString(model);
Fastjson convert POJO to json bytes
import com.alibaba.fastjson.JSON;
Model model = ...;
byte[] jsonBytes = JSON.toJSONBytes(model);
Fastjson write POJO as json string to OutputStream
import com.alibaba.fastjson.JSON;
Model model = ...;
OutputStream os;
JSON.writeJSONString(os, model);
Fastjson write POJO as json string to Writer
import com.alibaba.fastjson.JSON;
Model model = ...;
Writer writer = ...;
JSON.writeJSONString(writer, model);