API参考:dart:convert library
引入:
import 'dart:convert';
一、json
将字符串转换为对象:
import 'dart:convert' show json;
var jsonString = '''
[
{"score": 40},
{"score": 80}
]
''';
var scores = json.decode(jsonString);
assert(scores is List);
assert(scores[0] is Map);
assert(firstScore['score'] == 40);
print(scores); // [{score: 40}, {score: 80}]
将对象转换为字符串:
var scores = [
{'score': 40},
{'score': 80},
];
var jsonText = json.encode(scores);
print(jsonText); // [{"score":40},{"score":80}]
json.decode
可以写为jsonDecode
json.encode
可以写为jsonEncode
二、utf8
使用 utf8.encode
和 utf8.decode
编码和解码数据:
import 'dart:convert' show utf8;
var str = 'Hello world';
var strEncode = utf8.encode(str);
print(strEncode); // [72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]
print(strEncode is List<int>); // true
var strDecode = utf8.decode(strEncode);
print(strDecode == str); // true