flutter 如何去除[{"conditionId": "", "deviceId": 1663743185928585216, "ieeeAddress": "CC86ECFFFE5CDCD9", "endpoint": 1, "deviceType": "A010401000000", "state": 1, "temperature": 0, "humidity": 0, "cron": "*"}]的[]
时间: 2023-07-03 19:23:52 浏览: 86
你可以使用 `json.decode()` 将这个 JSON 字符串转换为 Dart 对象,然后将对象转换回 JSON 字符串时去掉数组外面的 `[]`,示例代码如下:
```dart
import 'dart:convert';
void main() {
final jsonString = '[{"conditionId": "", "deviceId": 1663743185928585216, "ieeeAddress": "CC86ECFFFE5CDCD9", "endpoint": 1, "deviceType": "A010401000000", "state": 1, "temperature": 0, "humidity": 0, "cron": "*"}]';
final List<dynamic> jsonList = json.decode(jsonString);
final jsonWithoutArray = jsonEncode(jsonList[0]); // 转成 JSON 字符串时去掉数组外面的 []
print(jsonWithoutArray); // 输出: {"conditionId":"","deviceId":1663743185928585216,"ieeeAddress":"CC86ECFFFE5CDCD9","endpoint":1,"deviceType":"A010401000000","state":1,"temperature":0,"humidity":0,"cron":"*"}
}
```
这样就可以得到一个不包含 `[]` 的 JSON 字符串了。
阅读全文