简介
FastJson是阿里巴巴开源的一个Json处理工具包,包括“序列化”和“反序列化”两部分。fastjson具有极快的性能,超越任其他的Java Json parser。包括自称最快的JackJson,功能强大,完全支持Java Bean、集合、Map、日期、Enum,支持范型,支持自省;无依赖,能够直接运行在Java SE 5.0以上版本;支持Android;开源 (Apache 2.0)。
FastJson解析配置
Jar包下载链接:https://repo1.maven.org/maven2/com/alibaba/fastjson/1.2.47/fastjson-1.2.47.jar
implementation files('libs/fastjson-1.2.47.jar')
JSON类
即所有操作都用JSON类,然后调用JSON类的相关静态方法。
fastjson常用方法
public static final String toJSONString(Object object); // 将object序列化为json字符串
public static final String toJSONString(Object object, boolean prettyFormat); // 将object序列化为带格式的json字符串
public static final JSONObject parseObject(String text); // 把JSON文本parse成JSONObject
代码
private void testMethod() {
Student student = new Student();
student.setAge("20");
student.setName("张三");
student.setSex("男");
String beanJson = JSON.toJSONString(student);
Log.d("TAG", "beanJson----:" + beanJson);
}
结果
beanJson----:{"age":"20","name":"张三","sex":"男"}
代码
private void testMethod() {
Map map = new HashMap();
map.put("age", "25");
map.put("name", "张三");
map.put("sex", "男");
String mapJson = JSON.toJSONString(map);
Log.d("TAG", "mapJson----:" + mapJson);
}
结果
mapJson----:{"age":"25","name":"张三","sex":"男"}
代码
private void testMethod() {
Student student1 = new Student();
student1.setAge("20");
student1.setName("张三");
student1.setSex("男");
Student student2 = new Student();
student2.setAge("22");
student2.setName("小华");
student2.setSex("女");
List list = new ArrayList();
list.add(student1);
list.add(student2);
String listJson = JSON.toJSONString(list);
Log.d("TAG", "listJson----:" + listJson);
}
结果
listJson----:[{"age":"20","name":"张三","sex":"男"},{"age":"22","name":"小华","sex":"女"}]
数据源
{"age":"20","name":"张三","sex":"男"}
代码
private void testMethod() {
com.alibaba.fastjson.JSONObject object=JSON.parseObject(string);
String age=object.getString("age");
String name=object.getString("name");
String sex=object.getString("sex");
Log.d("TAG","age----:"+age);
Log.d("TAG","name----:"+name);
Log.d("TAG","sex----:"+sex);
}
结果
age----:20
name----:张三
sex----:男
数据源
[{"age":"20","name":"张三","sex":"男"},{"age":"22","name":"小华","sex":"女"}]
代码
private void testMethod() {
com.alibaba.fastjson.JSONArray array = JSON.parseArray(string);
int length = array.size();
for (int i = 0; i < length; i++) {
com.alibaba.fastjson.JSONObject jsonObject = array.getJSONObject(i);
String ages = jsonObject.getString("age");
String names = jsonObject.getString("name");
String sexs = jsonObject.getString("sex");
Log.d("TAG", "ages----:" + ages);
Log.d("TAG", "names----:" + names);
Log.d("TAG", "sexs----:" + sexs);
}
}
结果
ages----:20
names----:张三
sexs----:男
ages----:22
names----:小华
sexs----:女
数据源
{"result":[{"sex":"男","age":"20","name":"张三"},{"sex":"女","age":"22","name":"小华"},{"sex":"男","age":"25","name":"李四"},{"sex":"女","age":"27","name":"小丽"}],"school":"清华大学","error":false}
代码
private void testMethod() {
com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(string);
com.alibaba.fastjson.JSONArray jsonArray = jsonObject.getJSONArray("result");
int lengths = jsonArray.size();
for (int i = 0; i < lengths; i++) {
com.alibaba.fastjson.JSONObject jsonObjects = jsonArray.getJSONObject(i);
String sexss = jsonObjects.getString("sex");
String agess = jsonObjects.getString("age");
String namess = jsonObjects.getString("name");
Log.d("TAG", "sexss----:" + sexss);
Log.d("TAG", "agess----:" + agess);
Log.d("TAG", "namess----:" + namess);
}
String school = jsonObject.getString("school");
Log.d("TAG", "school----:" + school);
boolean error = jsonObject.getBoolean("error");
Log.d("TAG", "error----:" + error);
}
结果
sexss----:男
agess----:20
namess----:张三
sexss----:女
agess----:22
namess----:小华
sexss----:男
agess----:25
namess----:李四
sexss----:女
agess----:27
namess----:小丽
school----:清华大学
error----:false
图解
即com.alibaba.fastjson.JSONObject有各种get方法,而不是只有getString();
附:fastjson GItHub官方链接
https://github.com/alibaba/fastjson