官网地址
https://github.com/alibaba/fastjson/wiki/Quick-Start-CN

- 什么是fastjson?
fastjson是阿里巴巴的开源JSON解析库,它可以解析JSON格式的字符串,支持将Java Bean序列化为JSON字符串,也可以从JSON字符串反序列化到JavaBean。
fastjson的API十分简洁。
json 基本值类型 true ,false, number,string, object,array, null
String text = JSON.toJSONString(obj); //序列化
VO vo = JSON.parseObject("{...}", VO.class); //反序列化
import com.alibaba.fastjson.JSON;
Model model = new Model();
model.id = 1001;
model.name = "gaotie";
Model model2 = new Model();
model2.id = 1002;
model2.name = "gaotie2";
Vector<Model> modelVec=new Vector<>();
modelVec.add(model);
modelVec.add(model2);
ModelBig modelBig=new ModelBig();
modelBig.id= 2001;
modelBig.name="bigname1";
modelBig.modelVec=modelVec;
// class 转 string
String jsonString1 = JSON.toJSONString(modelBig);
System.out.println(" Java对象 转换为 string ,打印String对象 为: " + jsonString1);
//Java对象 转换为 string ,打印String对象 为: {"id":2001,"modelVec":[{"id":1001,"name":"gaotie"},{"id":1002,"name":"gaotie2"}],"name":"bigname1"}
com.alibaba.fastjson.JSONObject jsonObject2 = (com.alibaba.fastjson.JSONObject) com.alibaba.fastjson.JSONObject.toJSON(modelBig);
System.out.println("Java对象转化为 object对象 强转为 JSONObject ,打印JSONObject 为: \n" + jsonObject2);//
//Java对象转化为 object对象 强转为 JSONObject ,打印JSONObject 为:
//{"id":2001,"name":"bigname1","modelVec":[{"id":1001,"name":"gaotie"},{"id":1002,"name":"gaotie2"}]}
// 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);
3.2 Java类库
Fastjson
Jackson
Gson
Xstream
本文介绍了阿里巴巴开源JSON解析库fastjson,它能解析JSON格式字符串,支持Java Bean与JSON字符串的序列化和反序列化,其API十分简洁,还给出了官网地址https://github.com/alibaba/fastjson/wiki/Quick-Start-CN 。
202

被折叠的 条评论
为什么被折叠?



