1. 程式人生 > 其它 >【HarmonyOS】【Json解析】ZSON 與 HiJson 使用

【HarmonyOS】【Json解析】ZSON 與 HiJson 使用

鴻蒙開發 - Json解析 - 官方ZSON 和 非官方的 HiJson 的使用

HiLog配置

為了方便除錯,檢視,先設定好Hilog

public static final HiLogLabel loglabel = new HiLogLabel(HiLog.LOG_APP,0x11102,"【xrilang】");

ZSONObject

 //Json測試2:使用ZSONObject(官方)
                //  1.將總Json字串轉為一個ZSONObject
                ZSONObject zsonObject1 = ZSONObject.stringToZSON(str);
                // 2.將所需要的內容從ZSONObject物件中取出
                //// 2.1 當所需要的內容是陣列時
                ////// 2.1.1 將所需陣列內容 從 ZSONObject 中取出來 轉換為 ZSONArray 得到的是一個新Json
                ZSONArray result1 = zsonObject1.getZSONArray("lives");
                ////// 2.1.2 將這個新Json轉為字串輸出看效果
                String result2 = result1.getString(0);
                HiLog.info(loglabel,"【ZSONObjec】result2::"+result2);
                //////// 【輸出結果】 {"adcode":"130500","city":"邢臺市","humidity":"98","province":"河北","reporttime":"2020-09-26 21:58:34","temperature":"17","weather":"多雲","winddirection":"北","windpower":"≤3"}
                ////-------
                ZSONObject zsonObject2 = ZSONObject.stringToZSON(result2);
                ////-------
                //// 2.2 當需要的內容是 普通資料型別時(例如String,int,double等)
                ////// 2.2.1 將所需內容取出,並存放在變數中,輸出檢視效果
                String  result3 = zsonObject2.getString("province");
                HiLog.info(loglabel,"【ZSONObjec】result3::"+result3);

HiJson(第三方)

獲取與配置Json

獲取HiJson

crazycodeboy/hijson: HiJson is a lightweight JSON parsing library that can be used for HarmonyOS, Android, and Java (github.com)

點選上方連結,複製如下圖的紅框內容

implementation 'org.devio.hi.json:hijson:1.0.0'

配置到專案檔案中

HiJson使用

準備好一段Json

jsonString:

{
    "status": "1",
    "count": "1",
    "info": "OK",
    "infocode": "10000",
    "lives": [
        {
            "province": "河北",
            "city": "邢臺市",
            "adcode": "130500",
            "weather": "多雲",
            "temperature": "17",
            "winddirection": "北",
            "windpower": "≤3",
            "humidity": "98",
            "reporttime": "2020-09-26 21:58:34"
        }
    ]
}

匯入HiJson包

import org.devio.hi.json.HiJson;

使用HiJson

//Json測試1:使用HiJson(第三方)
                HiJson hiJson = new HiJson(str).get("lives").get(0);
                String province = hiJson.value("province");
                String city = hiJson.value("city");
                HiLog.info(loglabel,"【HiJson】省市:"+province+"  城市:"+city);

執行結果

完整程式碼

MainAbility.java

package cc.mllt.eduxiancheng.slice;

import cc.mllt.eduxiancheng.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.utils.zson.ZSONArray;
import ohos.utils.zson.ZSONObject;
import org.devio.hi.json.HiJson;

public class MainAbilitySlice extends AbilitySlice {
    String str = "{\n" +
            "    \"status\": \"1\",\n" +
            "    \"count\": \"1\",\n" +
            "    \"info\": \"OK\",\n" +
            "    \"infocode\": \"10000\",\n" +
            "    \"lives\": [\n" +
            "        {\n" +
            "            \"province\": \"河北\",\n" +
            "            \"city\": \"邢臺市\",\n" +
            "            \"adcode\": \"130500\",\n" +
            "            \"weather\": \"多雲\",\n" +
            "            \"temperature\": \"17\",\n" +
            "            \"winddirection\": \"北\",\n" +
            "            \"windpower\": \"≤3\",\n" +
            "            \"humidity\": \"98\",\n" +
            "            \"reporttime\": \"2020-09-26 21:58:34\"\n" +
            "        }\n" +
            "    ]\n" +
            "}";
    public static final HiLogLabel loglabel = new HiLogLabel(HiLog.LOG_APP,0x11102,"【xrilang】");
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        //執行緒測試
        MyExecutor.runChild(new Runnable() {
            @Override
            public void run() {
                HiLog.info(loglabel,"MyExecutor:在非同步執行緒執行任務開始");
                //Json測試1:使用HiJson(第三方)
                HiJson hiJson = new HiJson(str).get("lives").get(0);
                String province = hiJson.value("province");
                String city = hiJson.value("city");
                HiLog.info(loglabel,"【HiJson】省市:"+province+"  城市:"+city);

                //Json測試2:使用ZSONObject(官方)
                //  1.將總Json字串轉為一個ZSONObject
                ZSONObject zsonObject1 = ZSONObject.stringToZSON(str);
                // 2.將所需要的內容從ZSONObject物件中取出
                //// 2.1 當所需要的內容是陣列時
                ////// 2.1.1 將所需陣列內容 從 ZSONObject 中取出來 轉換為 ZSONArray 得到的是一個新Json
                ZSONArray result1 = zsonObject1.getZSONArray("lives");
                ////// 2.1.2 將這個新Json轉為字串輸出看效果
                String result2 = result1.getString(0);
                HiLog.info(loglabel,"【ZSONObjec】result2::"+result2);
                //////// 【輸出結果】 {"adcode":"130500","city":"邢臺市","humidity":"98","province":"河北","reporttime":"2020-09-26 21:58:34","temperature":"17","weather":"多雲","winddirection":"北","windpower":"≤3"}
                ////-------
                ZSONObject zsonObject2 = ZSONObject.stringToZSON(result2);
                ////-------
                //// 2.2 當需要的內容是 普通資料型別時(例如String,int,double等)
                ////// 2.2.1 將所需內容取出,並存放在變數中,輸出檢視效果
                String  result3 = zsonObject2.getString("province");
                HiLog.info(loglabel,"【ZSONObjec】result3::"+result3);
                HiLog.info(loglabel,"MyExecutor:在非同步執行緒執行任務結束");
                MyExecutor.runMain(new Runnable() {
                    @Override
                    public void run() {
                        HiLog.info(loglabel,"MyExecutor:回到Main執行緒執行任務");
                    }
                });
            }
        });


    }


    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}

執行結果

MyExecutor.java

package cc.mllt.eduxiancheng.slice;

import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

public class MyExecutor {


 /**
     * 切換任務到主執行緒執行
     * @param runnable
     */
    public static void runMain(Runnable runnable) {
        //切換到主執行緒
        EventRunner runner = EventRunner.getMainEventRunner();
        EventHandler eventHandler = new EventHandler(runner);
        //執行任務
        eventHandler.postSyncTask(runnable);
    }

    /**
     * 在子執行緒執行任務
     * @param runnable
     */
    public static void runChild(Runnable runnable) {
        //開啟一個新執行緒
        EventRunner runner = EventRunner.create(true);
        EventHandler eventHandler = new EventHandler(runner);
        //執行任務
        eventHandler.postTask(runnable,0,EventHandler.Priority.IMMEDIATE);
    }
}

相關文章:【HarmonyOS】【多執行緒與併發】EventHandler - 萌狼藍天 - 部落格園 (cnblogs.com/mllt)

作者:萌狼藍天,轉載請註明原文連結:https://www.cnblogs.com/mllt/p/HarmonyOS_edu_note_JSON_Zson_HiJson.html | 萌狼藍天@嗶哩嗶哩 | QQ:3447902411(僅限技術交流,新增請說明方向)