android開發筆記之fastjson

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章