To convert a JSON string to a map in Dart, you can use the 'dart:convert' library, which provides the jsonDecode function to parse JSON data into a map.
We can convert a JSON string to a map in Dart by using the 'dart:convert' library's jsonDecode function to parse the JSON string and then converting the result into a Map.
For example,
jsonDecode
function to parse the JSON string into a map.import 'dart:convert';
void main() {
// Define a JSON string
String jsonString = '{"1": 10, "2": 20, "3": 30, "4": 40, "5": 50}';
// Parse the JSON string into a map
Map<String, dynamic> myMap = jsonDecode(jsonString);
// Print the resulting map
print('Map from JSON string:');
myMap.forEach((key, value) {
print('$key: $value');
});
}
Map from JSON string: 1: 10 2: 20 3: 30 4: 40 5: 50
In this tutorial, we learned How to Convert a JSON String to a Map in Dart language with well detailed examples.