utsjsonobject.md 2.8 KB
Newer Older
Q
qiang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
# UTSJSONObject

UTSJSONObject 是 UTS 语言的内置类型,主要用来操作[匿名对象](../object.md#anonymous-object)

## 创建实例

UTSJSONObject 对象的实例目前主要通过两种方式来创建:

* 通过[对象字面量](../literal.md#object-literal)

```ts
const person: UTSJSONObject = {
    name: 'Tom',
Q
qiang 已提交
14 15 16
    printName: () => {
      // ...
    }
Q
qiang 已提交
17 18 19 20 21 22
}
```

* 通过 JSON 字符串

```ts
Q
qiang 已提交
23
const person: UTSJSONObject = JSON.parse('{"name":"Tom"}')
Q
qiang 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
```

## 实例方法

### get(key: string): any | null

返回指定键对应的值,如果对象中不存在此键则返回 null。

```ts
const name: string = person.get('name') as string
```

get 方法可以简化为使用下标运算符 `[]` 访问

```ts
const name: string = person['name'] as string
```

### set(key: string, value: any | null)

增加或更新指定键对应的值。

```ts
Q
qiang 已提交
47
person.set('name', 'Tom')
Q
qiang 已提交
48 49 50 51 52
```

set 方法可以简化为使用下标运算符 `[]` 赋值

```ts
Q
qiang 已提交
53
person['name'] = 'Tom'
Q
qiang 已提交
54
```
D
DCloud_LXH 已提交
55
### getAny(key)
Q
qiang 已提交
56

D
DCloud_LXH 已提交
57
<!-- UTSJSON.UTSJSONObject.getAny.description -->
Q
qiang 已提交
58

D
DCloud_LXH 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
<!-- UTSJSON.UTSJSONObject.getAny.param -->

<!-- UTSJSON.UTSJSONObject.getAny.returnValue -->

<!-- UTSJSON.UTSJSONObject.getAny.compatibility -->

### getBoolean(key)

<!-- UTSJSON.UTSJSONObject.getBoolean.description -->

<!-- UTSJSON.UTSJSONObject.getBoolean.param -->

<!-- UTSJSON.UTSJSONObject.getBoolean.returnValue -->

<!-- UTSJSON.UTSJSONObject.getBoolean.compatibility -->

### getNumber(key)

<!-- UTSJSON.UTSJSONObject.getNumber.description -->

<!-- UTSJSON.UTSJSONObject.getNumber.param -->

<!-- UTSJSON.UTSJSONObject.getNumber.returnValue -->

<!-- UTSJSON.UTSJSONObject.getNumber.compatibility -->

### getString(key)

<!-- UTSJSON.UTSJSONObject.getString.description -->

<!-- UTSJSON.UTSJSONObject.getString.param -->

<!-- UTSJSON.UTSJSONObject.getString.returnValue -->

<!-- UTSJSON.UTSJSONObject.getString.compatibility -->

### getJSON(key)

<!-- UTSJSON.UTSJSONObject.getJSON.description -->

<!-- UTSJSON.UTSJSONObject.getJSON.param -->

<!-- UTSJSON.UTSJSONObject.getJSON.returnValue -->

<!-- UTSJSON.UTSJSONObject.getJSON.compatibility -->

### getArray(key)

<!-- UTSJSON.UTSJSONObject.getArray.description -->

<!-- UTSJSON.UTSJSONObject.getArray.param -->

<!-- UTSJSON.UTSJSONObject.getArray.returnValue -->

<!-- UTSJSON.UTSJSONObject.getArray.compatibility -->

### getArray(key)

<!-- UTSJSON.UTSJSONObject.getArray_1.description -->

<!-- UTSJSON.UTSJSONObject.getArray_1.param -->

<!-- UTSJSON.UTSJSONObject.getArray_1.returnValue -->

<!-- UTSJSON.UTSJSONObject.getArray_1.compatibility -->

### toMap()

<!-- UTSJSON.UTSJSONObject.toMap.description -->

<!-- UTSJSON.UTSJSONObject.toMap.param -->

<!-- UTSJSON.UTSJSONObject.toMap.returnValue -->
Q
qiang 已提交
132 133

```ts
lizhongyi_'s avatar
lizhongyi_ 已提交
134
person.toMap().forEach((value, key) => {
Q
qiang 已提交
135 136 137 138
    console.log(key)
    console.log(value)
})
```
D
DCloud_LXH 已提交
139 140

<!-- UTSJSON.UTSJSONObject.toMap.compatibility -->