Android數據解析JSON解析之FastJson解析

Android數據解析JSON解析之FastJson解析

概述

Fastjson是一個Java語言編寫的高性能功能完善的JSON庫。它采用一種“假定有序快速匹配”的算法,把JSON Parse的性能提升到極致,是目前Java語言中最快的JSON庫.Fastjson接口簡單易用,已經被廣泛使用在緩存序列化,協議交互,Web輸出,Android客戶端等多種應用場景。

數據解析操作

1、將json格式的對象轉換為Java對象

測試數據

{
"id":2, "name":"大蝦", 
"price":12.3, 
"imagePath":"https://192.168.10.165:8080/L05_Server/images/f1.jpg"
}

代碼:

private void jsonToJavaObjectByFastJson() {
    // 1 獲取或創建json數據
    String json = "{\n" +
            "\t\"id\":2, \"name\":\"大蝦\", \n" +
            "\t\"price\":12.3, \n" +
            "\t\"imagePath\":\"https://192.168.10.165:8080/L05_Server/images/f1.jpg\"\n" +
            "}\n";

    // 2 解析JSON數據
    ShopInfo shopInfo = JSON.parseObject(json, ShopInfo.class);
    // 3 顯示數據
    tvFastjsonOrignal.setText(json);
    tvFastjsonLast.setText(shopInfo.toString());
}

測試結果
這裡寫圖片描述

08-21 05:50:02.354 4874-4874/com.example.zhangdai.androidjson I/TAG: 詳細數據:
                                                                     id=2
                                                                     name=大蝦
                                                                     price=12.3
                                                                     imagePath=https://192.168.10.165:8080/L05_Server/images/f1.jpg

2、將Java對象轉換為json對象

測試數據

[
    {
        "id": 1,
        "imagePath": "https://192.168.10.165:8080/f1.jpg",
        "name": "大蝦1",
        "price": 12.3
    },
    {
        "id": 2,
        "imagePath": "https://192.168.10.165:8080/f2.jpg",
        "name": "大蝦2",
        "price": 12.5
    }
]

代碼

private void jsonToJavaListByFastJson() {

    String json = "[\n" +
            "    {\n" +
            "        \"id\": 1,\n" +
            "        \"imagePath\": \"https://192.168.10.165:8080/f1.jpg\",\n" +
            "        \"name\": \"大蝦1\",\n" +
            "        \"price\": 12.3\n" +
            "    },\n" +
            "    {\n" +
            "        \"id\": 2,\n" +
            "        \"imagePath\": \"https://192.168.10.165:8080/f2.jpg\",\n" +
            "        \"name\": \"大蝦2\",\n" +
            "        \"price\": 12.5\n" +
            "    }\n" +
            "]";

    // 2 解析JSON數據
    List shops=JSON.parseArray(json,ShopInfo.class);
    // 3 顯示數據
    tvFastjsonOrignal.setText(json);
    tvFastjsonLast.setText(shops.toString());

}

測試結果
這裡寫圖片描述

8-21 05:51:34.314 4874-4874/com.example.zhangdai.androidjson I/TAG: 詳細數據:
                                                                     id=1
                                                                     name=大蝦1
                                                                     price=12.3
                                                                     imagePath=https://192.168.10.165:8080/f1.jpg
08-21 05:51:34.314 4874-4874/com.example.zhangdai.androidjson I/TAG: 詳細數據:
                                                                     id=2
                                                                     name=大蝦2
                                                                     price=12.5
                                                                     imagePath=https://192.168.10.165:8080/f2.jpg

3、將json格式的數組[]轉換為Java對象的List

private void javaToJsonObjectByFastJson() {
    // 1 創建一個Java對象
    ShopInfo shopInfo = new ShopInfo(1, "鮑魚", 250.0, "baoyu");

    // 2 生成JSON數據
    String json=JSON.toJSONString(shopInfo);

    // 3 顯示數據
    tvFastjsonOrignal.setText(shopInfo.toString());
    tvFastjsonLast.setText(json);
}

測試結果
這裡寫圖片描述

4、將Java對象的List轉換為json數組[]

private void javaToJsonArrayByFastJson() {
    // 1 創建一個Java集合
    List shops = new ArrayList<>();

    ShopInfo baoyu = new ShopInfo(1, "鮑魚", 250.0, "baoyu");
    ShopInfo longxia = new ShopInfo(2, "龍蝦", 251.0, "longxia");

    shops.add(baoyu);
    shops.add(longxia);

    // 2 生成JSON數據
    String json = JSON.toJSONString(shops);

    // 3 顯示JSON數據
    tvFastjsonOrignal.setText(shops.toString());
    tvFastjsonLast.setText(json);
}

測試結果
這裡寫圖片描述

5、解析復雜數據

測試數據

{
    "data": {
        "count": 5,
        "items": [
            {
                "id": 45,
                "title": "堅果"
            },
            {
                "id": 132,
                "title": "炒貨"
            },
            {
                "id": 166,
                "title": "蜜餞"
            },
            {
                "id": 195,
                "title": "果脯"
            },
            {
                "id": 196,
                "title": "禮盒"
            }
        ]
    },
    "rs_code": "1000",
    "rs_msg": "success"
}

代碼

 /*
       解析復雜數據
        */
    public void fastjsonOfComplex(View view) {
        // 1 獲取或創建JSON數據
        String json = "{\n" +
                "    \"data\": {\n" +
                "        \"count\": 5,\n" +
                "        \"items\": [\n" +
                "            {\n" +
                "                \"id\": 45,\n" +
                "                \"title\": \"堅果\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"id\": 132,\n" +
                "                \"title\": \"炒貨\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"id\": 166,\n" +
                "                \"title\": \"蜜餞\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"id\": 195,\n" +
                "                \"title\": \"果脯\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"id\": 196,\n" +
                "                \"title\": \"禮盒\"\n" +
                "            }\n" +
                "        ]\n" +
                "    },\n" +
                "    \"rs_code\": \"1000\",\n" +
                "    \"rs_msg\": \"success\"\n" +
                "}";

        DataInfo dataInfo = parseObject(json, DataInfo.class);
        String mag = dataInfo.getRs_msg();
        Log.i("TAG", "mag:" + mag);
        String code = dataInfo.getRs_code();
        Log.i("TAG", "code:" + code);
        int count = dataInfo.getData().getCount();
        Log.i("TAG", "count:" + count);

        for (int i = 0; i < dataInfo.getData().getItems().size(); i++) {
            String title = dataInfo.getData().getItems().get(i).getTitle();
            int id = dataInfo.getData().getItems().get(i).getId();
            Log.i("TAG", "詳細數據:" + "\n"
                    + "id=" + id + "\n"
                    + "title=" + title + "\n"
            );
        }
        tvFastjsonOrignal.setText(json);
        tvFastjsonLast.setText(dataInfo.toString());
    }

測試效果
這裡寫圖片描述

08-21 05:55:53.250 4874-4874/com.example.zhangdai.androidjson I/TAG: mag:success
08-21 05:55:53.250 4874-4874/com.example.zhangdai.androidjson I/TAG: code:1000
08-21 05:55:53.250 4874-4874/com.example.zhangdai.androidjson I/TAG: count:5
08-21 05:55:53.250 4874-4874/com.example.zhangdai.androidjson I/TAG: 詳細數據:
                                                                     id=45
                                                                     title=堅果
08-21 05:55:53.250 4874-4874/com.example.zhangdai.androidjson I/TAG: 詳細數據:
                                                                     id=132
                                                                     title=炒貨
08-21 05:55:53.250 4874-4874/com.example.zhangdai.androidjson I/TAG: 詳細數據:
                                                                     id=166
                                                                     title=蜜餞
08-21 05:55:53.250 4874-4874/com.example.zhangdai.androidjson I/TAG: 詳細數據:
                                                                     id=195
                                                                     title=果脯
08-21 05:55:53.250 4874-4874/com.example.zhangdai.androidjson I/TAG: 詳細數據:
                                                                     id=196
                                                                     title=禮盒

測試Demo地址
github:https://github.com/TDCQZD/AndroidJson

You May Also Like