【HarmonyOS】【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);

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

HiJson(第三方)

获取与配置Json

获取HiJson

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

点击上方链接,复制如下图的红框内容

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

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

配置到项目文件中

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

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"
        }
    ]
}

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

导入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);

执行结果

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

完整代码

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);
    }
}

运行结果

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

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)

上一篇:如何使用 iCloud 钥匙串从 macOS Monterey 导入和导出密码?


下一篇:第三次实验