Reputation: 27
I am trying to retrieve a JSON from a URL link. The JSON data consist of both English and Burmese language. However, when I retrieve the data, the app shows a bunch of Burmese words converted into "áá". Any solutions?
class _GetJsonResultState extends State<GetJsonResult> {
@override
Widget build(BuildContext context) {
var jsonFileName = "assets/resultlist/resultlist.json";
fetchData() async {
final response =
await http.get('https://jsonkeeper.com/b/47QP');
if (response.statusCode == 200) {
return(response.body);
}
}
return FutureBuilder(
future: fetchData(),
builder: (context, snapshot) {
List myData = json.decode(snapshot.data.toString());
if (myData == null) {
return Scaffold(
body: Center(
// Loads upon null
child: CircularProgressIndicator(),
),
);
} else {
return Home(myData: myData);
}
},
);
}
}
This is what I'm supposed to get
Upvotes: 1
Views: 289
Reputation: 6786
Its a decoding issue. Just to clarify what is happening in background
The Dart http API defines (as mentioned below) two ways of interacting with the response data:
body → String
The body of the response as a string. This is converted from bodyBytes using the charset parameter of the Content-Type header field, if available. If it's unavailable or if the encoding name is unknown, latin1 is used by default, as per RFC 2616.
bodyBytes → Uint8List
The bytes comprising the body of this response.
More details at https://github.com/dart-lang/http/issues/175#issuecomment-415721621
So in such cases where you know encoding before hand and think its going to mess use request encoding and a decode from byte
fetchData() async {
final response = await http.get('https://jsonkeeper.com/b/47QP',
headers: {"charset": "utf-8", "Accept-Charset": "utf-8"});
if (response.statusCode == 200) {
return (utf8.decode(response.bodyBytes));
}
}
Upvotes: 1
Reputation: 1014
Change your fetchData() function as below
fetchData() async {
final response = await http.get('https://jsonkeeper.com/b/47QP',
headers: {"charset": "utf-8", "Accept-Charset": "utf-8"});
if (response.statusCode == 200) {
return (utf8.decode(response.bodyBytes));
}
}
Upvotes: 1