Complete the code to parse a JSON string into a Dart map using jsonDecode.
import 'dart:convert'; void main() { String jsonString = '{"name": "Alice", "age": 30}'; var user = [1](jsonString); print(user['name']); }
The jsonDecode function converts a JSON string into a Dart object, such as a Map.
Complete the code to access the 'age' value from the parsed JSON map.
import 'dart:convert'; void main() { String jsonString = '{"name": "Bob", "age": 25}'; var user = jsonDecode(jsonString); int age = user[1]; print(age); }
To get a value from a Map in Dart, use square brackets with the key as a string.
Fix the error in the code to correctly parse a JSON list of strings.
import 'dart:convert'; void main() { String jsonString = '["apple", "banana", "cherry"]'; List<String> fruits = List<String>.from([1](jsonString)); print(fruits[1]); }
Use jsonDecode to convert the JSON string into a Dart List, then convert it to List<String>.
Fill both blanks to parse a JSON string and access the first user's email.
import 'dart:convert'; void main() { String jsonString = '{"users": [{"email": "a@example.com"}, {"email": "b@example.com"}]}'; var data = [1](jsonString); String email = data['users'][2]['email']; print(email); }
First, decode the JSON string with jsonDecode. Then access the first element of the 'users' list with [0].
Fill all three blanks to parse a JSON string and create a map of user names to ages for users older than 20.
import 'dart:convert'; void main() { String jsonString = '{"users": [{"name": "Anna", "age": 22}, {"name": "Ben", "age": 19}]}'; var data = jsonDecode(jsonString); var result = {user[1]: user[2] for user in data['users'] if user['age'] [3] 20}; print(result); }
Use user['name'] as the key and user['age'] as the value. Filter users with age greater than 20 using >.