To convert a map to a JSON string in Dart, you can use the jsonEncode
function from the dart:convert
library. This function converts a Dart object (such as a map) into a JSON string.
We can convert a map to a JSON string in Dart using the jsonEncode
function from the dart:convert
library.
For example,
dart:convert
library.myMap
with key-value pairs.jsonEncode
function.print
function.import 'dart:convert';
void main() {
// Declare and initialize a map
var myMap = {
1: 10,
2: 20,
3: 30,
4: 40,
5: 50
};
// Convert the map to a JSON string
var jsonString = jsonEncode(myMap);
// Print the resulting JSON string
print('JSON string from map: $jsonString');
}
JSON string from map: {"1":10,"2":20,"3":30,"4":40,"5":50}
In this tutorial, we learned How to Convert a Map to a JSON String in Dart language with well detailed examples.