2021-01-12 14:29:47 +08:00
|
|
|
## JSON 扩展函数
|
|
|
|
|
|
|
|
### 1.json\_load
|
2021-01-13 15:49:25 +08:00
|
|
|
|
|
|
|
> 加载 json 数据。
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
|
|
|
```js
|
|
|
|
json_load(str) => object
|
|
|
|
json_load(binary) => object
|
|
|
|
json_load(data, size) => object
|
|
|
|
```
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```js
|
2022-08-23 10:34:55 +08:00
|
|
|
var a = json_load('{"name": "fscript", "age": 100}');
|
2022-08-23 10:12:36 +08:00
|
|
|
```
|
|
|
|
|
2021-01-12 14:29:47 +08:00
|
|
|
### 2.json\_save
|
2021-01-13 15:49:25 +08:00
|
|
|
|
2021-07-29 14:34:17 +08:00
|
|
|
> 将对象保存为 json 数据。
|
2021-01-13 15:49:25 +08:00
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
|
|
|
```js
|
2021-07-29 14:34:17 +08:00
|
|
|
json_save(obj) => binary
|
2021-01-13 15:49:25 +08:00
|
|
|
```
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```js
|
2022-08-23 10:34:55 +08:00
|
|
|
var a = json_load('{"name": "fscript", "age": 100}');
|
2022-08-23 10:12:36 +08:00
|
|
|
json_save(a);
|
|
|
|
```
|
|
|
|
|
2021-09-09 15:10:12 +08:00
|
|
|
### 3.json\_save\_to\_string
|
|
|
|
|
|
|
|
> 将对象保存为字符串数据。
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
|
|
|
```js
|
|
|
|
json_save_to_string(obj) => string
|
|
|
|
```
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```js
|
2022-08-23 10:34:55 +08:00
|
|
|
var a = json_load('{"name": "fscript", "age": 100}');
|
2022-08-23 10:12:36 +08:00
|
|
|
print(json_save_to_string(a));
|
|
|
|
```
|
|
|
|
|
2021-09-09 15:10:12 +08:00
|
|
|
### 4.ubjson\_load
|
2021-01-13 15:49:25 +08:00
|
|
|
|
|
|
|
> 加载 ubjson 数据。
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
|
|
|
```js
|
|
|
|
ubjson_load(binary) => object
|
|
|
|
ubjson_load(data, size) => object
|
|
|
|
```
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```js
|
2022-08-23 10:34:55 +08:00
|
|
|
var a = ubjson_load(file_read_binary("test.ubjson"));
|
2022-08-23 10:12:36 +08:00
|
|
|
```
|
|
|
|
|
2021-09-09 15:10:12 +08:00
|
|
|
### 5.ubjson\_save
|
2021-01-12 14:29:47 +08:00
|
|
|
|
2021-07-29 14:34:17 +08:00
|
|
|
> 将对象保存为 ubjson 数据。
|
2021-01-13 15:49:25 +08:00
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
|
|
|
```js
|
2021-07-29 14:34:17 +08:00
|
|
|
ubjson_save(obj) => binary
|
2021-01-13 15:49:25 +08:00
|
|
|
```
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```js
|
2022-08-23 10:34:55 +08:00
|
|
|
var a = json_load('{"name": "fscript", "age": 100}');
|
|
|
|
var b = ubjson_save(a);
|
2022-08-23 10:12:36 +08:00
|
|
|
```
|
|
|
|
|
2021-01-13 15:49:25 +08:00
|
|
|
### 完整示例
|
|
|
|
|
2021-01-12 14:29:47 +08:00
|
|
|
```js
|
|
|
|
//load from string
|
2022-08-23 18:27:17 +08:00
|
|
|
var str='{
|
2022-04-28 17:16:05 +08:00
|
|
|
"results": [
|
|
|
|
{
|
|
|
|
"location": {
|
|
|
|
"id": "WS0E9D8WN298",
|
|
|
|
"name": "GuangZhou"
|
|
|
|
},
|
|
|
|
"now": {
|
|
|
|
"code": "9",
|
|
|
|
"temperature": "30"
|
|
|
|
},
|
|
|
|
"last_update": "2022-04-28T14:50:00+08:00"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"name": "fscript",
|
|
|
|
"age": 100
|
2021-09-09 15:10:12 +08:00
|
|
|
}'
|
2022-08-23 18:27:17 +08:00
|
|
|
var a = json_load(str)
|
2021-01-12 14:29:47 +08:00
|
|
|
|
2022-04-28 17:16:05 +08:00
|
|
|
assert(object_get(a, "results.[0].location.id")=="WS0E9D8WN298");
|
|
|
|
assert(object_get(a, "results.[0].location.name")=="GuangZhou");
|
|
|
|
assert(object_get(a, "results.[0].now.code")=="9");
|
|
|
|
assert(object_get(a, "results.[0].now.temperature")=="30");
|
|
|
|
assert(object_get(a, "results.[0].last_update")=="2022-04-28T14:50:00+08:00");
|
2021-01-12 14:29:47 +08:00
|
|
|
assert(object_get(a, "name")=="fscript");
|
|
|
|
assert(object_get(a, "age")==100);
|
|
|
|
|
2023-09-05 16:37:38 +08:00
|
|
|
// 获取数组类型节点的子元素个数
|
|
|
|
assert(object_get(a, "results.#size")==1);
|
|
|
|
|
2021-01-12 14:29:47 +08:00
|
|
|
//save to file
|
2022-08-23 18:27:17 +08:00
|
|
|
var b=json_save(a)
|
2021-01-12 14:29:47 +08:00
|
|
|
assert(file_write("test.json", b))
|
|
|
|
|
2021-09-09 15:10:12 +08:00
|
|
|
//json obj to string
|
2022-08-23 18:27:17 +08:00
|
|
|
var c=json_save_to_string(a)
|
2022-04-28 17:16:05 +08:00
|
|
|
print(c)
|
2021-09-09 15:10:12 +08:00
|
|
|
|
2021-01-12 14:29:47 +08:00
|
|
|
//load from file as text
|
2022-08-23 18:27:17 +08:00
|
|
|
a = json_load(file_read_text("test.json"))
|
2021-01-12 14:29:47 +08:00
|
|
|
|
|
|
|
assert(object_get(a, "name")=="fscript");
|
|
|
|
assert(object_get(a, "age")==100);
|
|
|
|
|
|
|
|
//load from file as binary
|
2022-08-23 18:27:17 +08:00
|
|
|
a = json_load(file_read_binary("test.json"))
|
2021-01-12 14:29:47 +08:00
|
|
|
assert(file_remove("test.json"))
|
|
|
|
|
|
|
|
assert(object_get(a, "name")=="fscript");
|
|
|
|
assert(object_get(a, "age")==100);
|
|
|
|
|
|
|
|
assert(file_remove("test.json"))
|
|
|
|
unset(a)
|
|
|
|
unset(b)
|
2021-09-09 15:10:12 +08:00
|
|
|
unset(c)
|
|
|
|
unset(str)
|
2021-01-12 14:29:47 +08:00
|
|
|
```
|