【HarmonyOS】【Json解析】ZSON 与 HiJson 使用
生活随笔
收集整理的這篇文章主要介紹了
【HarmonyOS】【Json解析】ZSON 与 HiJson 使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
HiLog配置
為了方便調試,查看,先設置好Hilog
public static final HiLogLabel loglabel = new HiLogLabel(HiLog.LOG_APP,0x11102,"【xrilang】");ZSONObject
//Json測試2:使用ZSONObject(官方)// 1.將總Json字符串轉為一個ZSONObjectZSONObject zsonObject1 = ZSONObject.stringToZSON(str);// 2.將所需要的內容從ZSONObject對象中取出2.1 當所需要的內容是數組時// 2.1.1 將所需數組內容 從 ZSONObject 中取出來 轉換為 ZSONArray 得到的是一個新JsonZSONArray 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】");@Overridepublic void onStart(Intent intent) {super.onStart(intent);super.setUIContent(ResourceTable.Layout_ability_main);//線程測試MyExecutor.runChild(new Runnable() {@Overridepublic 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字符串轉為一個ZSONObjectZSONObject zsonObject1 = ZSONObject.stringToZSON(str);// 2.將所需要的內容從ZSONObject對象中取出2.1 當所需要的內容是數組時// 2.1.1 將所需數組內容 從 ZSONObject 中取出來 轉換為 ZSONArray 得到的是一個新JsonZSONArray 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() {@Overridepublic void run() {HiLog.info(loglabel,"MyExecutor:回到Main線程執行任務");}});}});}@Overridepublic void onActive() {super.onActive();}@Overridepublic 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)
總結
以上是生活随笔為你收集整理的【HarmonyOS】【Json解析】ZSON 与 HiJson 使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux操作寄存器前为什么要iorem
- 下一篇: 如何把Linux工具里的“军刀”Busy