Dart supports handling JSON.
You need to import convert library.
Decoding JSON String
jsonDecode() method converts JSON string into List that is dynamic type.
import 'dart:convert';
void main() {
var jsonString = '''
[{"key1": "value1"}, {"key2": "value2"}]
''';
var decodedString = jsonDecode(jsonString);
print(decodedString is List); // true
print(decodedString[0]); // {key1: value1}
print(decodedString[0] is Map); // true
print(decodedString[1]["key2"]); // value2
}
Encoding to JSON String
jsonEncode() method converts List data into JSON string.
import 'dart:convert';
void main() {
var jsonData = [
{"key1": "value1"},
{"key2": "value2"}
];
var encodedString = jsonEncode(jsonData);
print(encodedString);
// [{"key1":"value1"},{"key2":"value2"}]
}
'Dart (Flutter)' 카테고리의 다른 글
[Flutter] Adding Image to Flutter Application (0) | 2022.03.26 |
---|---|
[Flutter] Life Cycle (0) | 2022.03.23 |
[Dart] Data Communication with Stream (0) | 2022.03.20 |
[Flutter] Getting Started on Android Studio (0) | 2022.02.27 |
[Flutter] Getting Started on VSCode (0) | 2022.02.27 |
댓글