Flutter: Parsing Json with special characters (utf-8)

Viewed 1200

I have a pretty basic php code that generates a JSON file with some data:

$json = json_encode($theData, JSON_PRETTY_PRINT | JSON_FORCE_OBJECT | JSON_UNESCAPED_UNICODE);
file_put_contents('api.json', $json);

When I parse it in my Flutter app, it returns special characters and not characters like "éóáêô".

I was parsing json file in Flutter like this:

var data = await http.get("xxxx/api.json", headers: {'Content-Type': 'application/json'});
if (data.statusCode == 200) {
    print('Status Code 200: Ok!');
    var jsonData = json.decode(data.body);

Then, I figure out that changing:

json.decode(data.body);

by:

json.decode(utf8.decode(data.bodyBytes));

Solved the problem!

Question is: Why using utf8.decode and bodyBytes solved the problem? Am I creating the JSON file with PHP properly?

0 Answers
Related