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(僅限技術交流,添加請說明方向)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/379091.html
標籤:其他
上一篇:軟體測驗實戰教程系列(一)你知道怎么做好介面測驗?|看了就會
下一篇:Python編程環境設定
