Dart 中如何解析 JSON 对象 (JSONObject)
在 Dart 中,可以使用 dart:convert 库中的 jsonDecode 方法将 JSON 字符串转换为对象,然后使用 Map 或 List 来表示 JSONObject。
例如,如果有以下 JSON 字符串:
{
'name': 'John',
'age': 30,
'city': 'New York'
}
可以使用以下代码将其转换为 Map:
import 'dart:convert';
void main() {
String jsonString = '{"name": "John", "age": 30, "city": "New York"}';
Map<String, dynamic> jsonObject = jsonDecode(jsonString);
print(jsonObject);
}
输出结果:
{ name: John, age: 30, city: New York }
注意,在 Dart 中,JSON 对象的键必须是字符串类型,因此在使用 Map 表示 JSONObject 时,键的类型应该是 String。
原文地址: https://www.cveoy.top/t/topic/oXl5 著作权归作者所有。请勿转载和采集!