Component - JSON parsing (jsonDecode)
This Flutter widget shows how to parse a JSON string using jsonDecode and display the parsed data on screen. It demonstrates converting a JSON string into a Dart map and using the data in the UI.
This Flutter widget shows how to parse a JSON string using jsonDecode and display the parsed data on screen. It demonstrates converting a JSON string into a Dart map and using the data in the UI.
Scaffold
├─ AppBar
│ └─ Text
└─ Center
└─ Column
├─ Text (Name)
└─ Text (Age)jsonDecode function do in Flutter?jsonDecode function takes a JSON formatted string and parses it into Dart objects such as Map or List.jsonEncode, not jsonDecode.jsonDecode in Flutter?jsonDecode function is part of the dart:convert library.jsonDecode, you must import dart:convert with import 'dart:convert';.import 'dart:convert';
void main() {
String jsonString = '{"name":"Anna","age":25}';
var user = jsonDecode(jsonString);
print(user['age']);
}jsonDecode converts the JSON string into a Map with keys 'name' and 'age'.user['age'] returns the integer 25.import 'dart:convert';
void main() {
String jsonString = '{name:"John", age:30}';
var data = jsonDecode(jsonString);
print(data['name']);
}jsonDecode will throw a FormatException.String jsonString = '[{"id":1,"name":"Alice"},{"id":2,"name":"Bob"}]';
How do you parse it to get the name of the second user in Flutter?jsonDecode converts it to a List of Maps.users[1]['name'] gives 'Bob'.Pass the test and this pattern moves unknown