Introduction
JSON should be the most common data format in our daily use. In many cases, we need to convert an object into JSON format, it can also be said that we need to encode the object into JSON.
Although all characters in dart are stored in UTF-16, the more common format should be UTF-8. The same dart also provides UTF-8 encoding support.
All of this is contained in the dart:convert package.
If you want to use the condot package, simply import it:
import 'dart:convert';
Encode and decode JSON
The first thing to note is that although dart can use single quotes or double quotes to express strings, in json, the string must be expressed in double quotes, otherwise it is not a real json. You must pay attention to this. , So we need to define JSON like this:
var studentJson = '''
[
{"name": "jack"},
{"age": 18}
]
''';
Instead of this:
var studentJson = '''
[
{'name': 'jack'},
{'age': 18}
]
''';
If you want to convert the json string into an object, you can use the jsonDecode method in the convert package:
var studentList = jsonDecode(studentJson);
assert(studentList is List);
var student = studentList[0];
assert(student is Map);
assert(student['name'] == "jack");
In addition to decode, you can also encode objects into Json strings:
var studentList = [
{"name": "jack"},
{"age": 18}
];
var studentString = jsonEncode(studentList);
assert(studentString ==
'[{"name":"jack"},{"age":18}]');
The above object is just a simple object, which can be directly converted into JSON. What if it is a complex object?
For example, if an object is nested in an object, will the embedded object be converted into JSON?
Dart takes this problem into consideration, so there is a second parameter in the jsonEncode method, which indicates how to convert an object that cannot be directly encoded into an object that can be encoded:
String jsonEncode(Object? object,
{Object? toEncodable(Object? nonEncodable)?}) =>
json.encode(object, toEncodable: toEncodable);
If the second parameter is omitted, the .toJson() method of the corresponding object will be called.
UTF-8 encoding and decoding
First look at the decoding method of UTF-8:
String decode(List<int> codeUnits, {bool? allowMalformed})
The first parameter is to pass in a UTF-8 codeUnits array, and the second parameter indicates whether to replace the character sequence U+FFFD
Unicode replacement character. If false is passed in, FormatException will be thrown when such characters are encountered.
Look at an example of use:
List<int> utf8Bytes = [119, 119, 119, 46, 102, 108, 121, 100, 101, 97, 110, 46, 99, 111, 109];
var site = utf8.decode(utf8Bytes);
assert(site == 'www.flydean.com');
Correspondingly, you can use encode to encode strings or other objects:
print(utf8.encode('www.flydean.com'));
Summarize
The above dart supports json and UTF-8.
This article has been included in http://www.flydean.com/19-dart-decode-encode/
The most popular interpretation, the most profound dry goods, the most concise tutorial, and many tips you don't know are waiting for you to discover!
Welcome to pay attention to my official account: "programs, those things", know the technology, know you better!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。