fastjson
fastjson是阿里巴巴的开源JSON解析库,它可以解析JSON格式的字符串,支持将Java Bean序列化为JSON字符串,也可以从JSON字符串反序列化到JavaBean。
https://github.com/alibaba/fastjson/wiki/Quick-Start-CN
新手指南
https://github.com/alibaba/fastjson
fastjson github官网
fastjson相对其他JSON库的特点是快,从2011年fastjson发布1.1.x版本之后,其性能从未被其他Java实现的JSON库超越。
fastjson的API十分简洁。
String text = JSON.toJSONString(obj); //序列化
VO vo = JSON.parseObject("{...}", VO.class); //反序列化
demo
我是使用android studio测试验证:
(1)app\build.gradle
dependencies {
implementation 'com.alibaba:fastjson:1.2.61'
implementation 'com.alibaba:fastjson:1.1.71.android'
}
(2)MainActivity.java
package com.example.firstapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import com.alibaba.fastjson.JSON;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private final String jsonFile1 = "test.json";
private final String jsonFile2 = "test_list.json";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
testAlibabaFastjson();
}
private void testAlibabaFastjson() {
Address address = new Address();
address.setCity("city_01");
address.setProvince("province_01");
Student student = new Student();
student.setAddress(address);
student.setAge(10);
student.setName("name_01");
student.setSchool("school_01");
Log.i(TAG,"=========================01====================================");
Log.i(TAG,"JSON.toJSONString(address):" + JSON.toJSONString(address));
Log.i(TAG,"JSON.toJSONString(student):" + JSON.toJSONString(student));
Log.i(TAG,"=========================02====================================");
Address address1 = JSON.parseObject(JSON.toJSONString(address),Address.class);
Log.i(TAG,"address1:"+address1);
Student student1 = JSON.parseObject(JSON.toJSONString(student),Student.class);
Log.i(TAG,"student1:"+student1);
Log.i(TAG,"=========================03====================================");
ArrayList<Student> arrayList;
String jsonFile1String = GsonHelper.getAssetsJson(this,jsonFile1);
Log.i(TAG,"jsonFile1String:" + jsonFile1String);
arrayList = (ArrayList<Student>) JSON.parseArray(jsonFile1String,Student.class);
for (int i = 0;i < arrayList.size();i++){
Log.i(TAG,"arrayList.get(i):" + arrayList.get(i));
}
Log.i(TAG,"=========================04====================================");
String jsonFile2String = GsonHelper.getAssetsJson(this,jsonFile2);
Log.i(TAG,"jsonFile2String:" + jsonFile2String);
arrayList = (ArrayList<Student>) JSON.parseArray(jsonFile2String,Student.class);
for (int i = 0;i < arrayList.size();i++){
Log.i(TAG,"arrayList.get(i):" + arrayList.get(i));
}
}
}
测试输出日志:
I/MainActivity: =========================01====================================
I/MainActivity: JSON.toJSONString(address):{"city":"city_01","province":"province_01"}
I/MainActivity: JSON.toJSONString(student):{"address":{"city":"city_01","province":"province_01"},"age":10,"booleanKey":false,"name":"name_01","school":"school_01"}
I/MainActivity: =========================02====================================
address1:"province": province_01, "city": city_01
student1:"name": name_01,
"age": 10,
"booleanKey": false,
"school": school_01,
"address": "province": province_01, "city": city_01
I/MainActivity: =========================03====================================
jsonFile1String:
[
{
"name":"Tom",
"age":13,
"booleanKey":true,
"school":"T-school",
"address":{"province":"广东","city":"惠州"}
}
]
I/MainActivity: arrayList.get(i):"name": Tom,
"age": 13,
"booleanKey": true,
"school": T-school,
"address": "province": 广东, "city": 惠州
I/MainActivity: =========================04====================================
I/MainActivity: jsonFile2String:
[
{
"name":"Tom",
"age":13,
"booleanKey":true,
"school":"T-school",
"address":{"province":"广东","city":"惠州"}
},
{
"name":"Ketty",
"age":23,
"booleanKey":false,
"school":"K-school",
"address":{"province":"广东","city":"深圳"}
},
{
"name":"John",
"age":15,
"booleanKey":true,
"school":"J-school",
"address":{"province":"广东","city":"广州"}
}
]
I/MainActivity: arrayList.get(i):"name": Tom,
"age": 13,
"booleanKey": true,
"school": T-school,
"address": "province": 广东, "city": 惠州
I/MainActivity: arrayList.get(i):"name": Ketty,
"age": 23,
"booleanKey": false,
"school": K-school,
"address": "province": 广东, "city": 深圳
I/MainActivity: arrayList.get(i):"name": John,
"age": 15,
"booleanKey": true,
"school": J-school,
"address": "province": 广东, "city": 广州
参考
1.android开发笔记之gson
https://blog.csdn.net/hfreeman2008/article/details/105587834