“This is the 27th day of my participation in the Gwen Challenge in November. See details of the event: The Last Gwen Challenge in 2021”

Introduction to the

Json is probably the most common data format we use every day. Many times, we need to convert an object to JSON format, or encode it as JSON.

Although all characters in DART are stored in UTF-16, the more common format is UTF-8, which dart also provides encoding support for UTF-8.

All of this is contained in the Dart: Convert package.

To use the convet package, simply introduce:

import 'dart:convert';
Copy the code

Encode and decode JSON

The first thing to note is that while dart can use single or double quotation marks to represent strings, it’s important to note that strings in JSON must be double quotation marks, otherwise they are not real JSON, so we need to define JSON like this:

var studentJson = '''
  [
    {"name": "jack"},
    {"age": 18}
  ]
''';
Copy the code

Instead of this:

var studentJson = '''
  [
    {'name': 'jack'},
    {'age': 18}
  ]
''';
Copy the code

To convert a 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");
Copy the code

In addition to decode, you can also encode an object as a Json string:

var studentList = [
  {"name": "jack"},
  {"age": 18}
];

var studentString = jsonEncode(studentList);
assert(studentString ==
    '[{"name":"jack"},{"age":18}]');
Copy the code

The above object is just a simple object that can be converted directly to JSON. What if it’s a complex object?

For example, nested objects within objects, will the embedded objects also be converted to JSON?

Dart takes this into account, so there is a second parameter in the jsonEncode method that shows how to convert an object that is not directly encodeable into one that is:

String jsonEncode(Object? object, {Object? toEncodable(Object? nonEncodable)? }) => json.encode(object, toEncodable: toEncodable);Copy the code

If the second argument is ignored, the corresponding object’s.tojson () method is called.

Utf-8 encoding and decoding

Utf-8 decoding method

 String decode(List<int> codeUnits, {bool? allowMalformed})
Copy the code

The first argument is passed in an array of UTF-8 codeUnits, and the second argument indicates whether to replace the character sequence U+FFFD for Unicode replacement characters. If false is passed, a FormatException is thrown when such a character is encountered.

Look at an example in 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');
Copy the code

Instead, use encode to encode strings or other objects:

print(utf8.encode('www.flydean.com'));
Copy the code

conclusion

Dart support for JSON and UTF-8 above.

This article is available at www.flydean.com/19-dart-dec…

The most popular interpretation, the most profound dry goods, the most concise tutorial, many tips you didn’t know waiting for you to discover!

Welcome to pay attention to my public number: “procedures those things”, understand technology, more understand you!