Dart 中解析 JSON 对象:完整指南
在 Dart 中,你可以使用 dart:convert 库中的 jsonDecode 方法将 JSON 字符串转换为 Dart 对象。然后,你可以使用 Map 来表示 JSONObject。
例如:
import 'dart:convert';
void main() {
String jsonString = '{"name": "John", "age": 30}';
Map<String, dynamic> data = jsonDecode(jsonString);
print(data['name']); // John
print(data['age']); // 30
}
在这个例子中,我们首先使用 jsonDecode 方法将 JSON 字符串转换为 Dart 对象。然后,我们使用 Map 来表示 JSONObject。注意,我们在 Map 的类型声明中使用了 dynamic,这是因为 JSONObject 中的值可以是任何类型。你也可以使用具体的类型,如 Map<String, String>,如果你知道 JSONObject 中的值的类型。
原文地址: https://www.cveoy.top/t/topic/oXmn 著作权归作者所有。请勿转载和采集!