仓颉应用开发之——Json包使用示例(19)

一 概述

  • JsonArray示例
  • JsonValue和String相互转换
  • JsonValue和DataModel转换

二 JsonArray示例

1-示例代码

1
2
3
4
5
6
7
8
9
10
protected override func onPageShow() {

var list = ArrayList<JsonValue>();
list.append(JsonBool(true))
list.append(JsonInt(7363))
list.append(JsonString("abc"))
var jsa= JsonArray(list);
Hilog.printInfo("printInfo", jsa.toString())

}

2-打印消息

1
[true,7363,"abc"]

三 JsonValue和String相互转换

1-示例代码

1
2
3
4
5
6
7
8
9
10
11
12
protected override func onPageShow() {

//1-string->JsonValue
var str:String = ##"{"name": "zs","age": 30}"##;
var jv: JsonValue = JsonValue.fromStr(str)
//2-JsonValue->String
var res = jv.toString()
var prettyres = jv.toJsonString()
Hilog.printInfo("printInfo",res)
Hilog.printInfo("printInfo",prettyres)

}

2-打印消息(一个是toString,一个是toJsonString)

1
2
3
4
5
{"name":"zs","age":30}
{
"name": "zs",
"age": 30
}

四 JsonValue和DataModel转换

1-示例代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
protected override func onPageShow() {

//1-string->JsonValue->DataModel->Bean
var js = ##"{"name": "A","age": 30}"##
var jv = JsonValue.fromStr(js)
var dm = DataModel.fromJson(jv)
var A = Person.deserialize(dm)
Hilog.printInfo("printInfo","name == ${A.name}")
Hilog.printInfo("printInfo","age == ${A.age}")
//2-Bean->DataModel->JsonValue->JsonObject
dm = A.serialize()
var jo = dm.toJson().asObject()
Hilog.printInfo("printInfo",jo.toJsonString())

}

2-打印消息

1
2
3
4
5
6
name == A
age == 30
{
"name": "A",
"age": 30
}

五 参考

  • HarmonyOS NEXT Developer Beta5仓颉—Json相关示例