flutter jsonDecode UTF8
In Flutter, you can use the jsonDecode
function from the dart:convert
package to decode a JSON string into a Dart object. By default, jsonDecode
assumes that the input string is encoded in UTF-8.
To use jsonDecode
, you first need to import the dart:convert
package in your Flutter project:
import 'dart:convert';
Then, you can call the jsonDecode
function and pass in the JSON string as a parameter. Here's an example:
String jsonString = '{"name": "John", "age": 30}';
Map<String, dynamic> jsonMap = jsonDecode(jsonString);
In this example, jsonString
is the JSON string that you want to decode, and jsonMap
is a Map
object that will hold the decoded JSON data. Note that the type of jsonMap
is Map<String, dynamic>
, which means it can hold key-value pairs where the keys are String
s and the values can be of any type.
After decoding the JSON string, you can access the values in the jsonMap
using the keys. For example:
String name = jsonMap['name'];
int age = jsonMap['age'];
In this case, name
will be assigned the value 'John'
, and age
will be assigned the value 30
.
That's it! You have successfully used jsonDecode
to decode a JSON string in Flutter and access the values in the decoded JSON data.