Reputation: 29
So I'm getting JSON data from WordPress's REST API in Dart with this code:
import 'package:http/http.dart' as http;
import 'dart:convert';
Future<List> fetchWpPosts() async {
var url = Uri.parse('https://example.com/wp-json/wp/v2/posts');
final response = await http
.get(url, headers: {'Accept': 'application/json; charset=UTF-16'});
var convertDatatoJson = jsonDecode(utf8.decode(response.bodyBytes));
return convertDatatoJson; }
And this code is doing the job, except some characters still show as Unicode. On the website they show as they should.
Here's a pic of the emulator
And here is the JSON code that I target:
"title": {
"rendered":
"АСДФГХЙКЛЖ”ШЩ>?Ч!@№$%€§*()_——“ENG”________асд”асд”"
},
JSON obviously encode only some characters in unicode - that is only some " and ', onters " and ' show as they should. Then my code gets them and shows them as they are without convert them to utf-8.
Can I fix this in Dart or I have to do something to the wordpress site?
Thank you!
Upvotes: 0
Views: 651
Reputation: 433
I solved this problem like that
var response = await http.get(url).timeout(const Duration(minutes: 3));
const utf8Decoder = Utf8Decoder(allowMalformed: true);
final decodedBytes = utf8Decoder.convert(response.bodyBytes);
```
Upvotes: 1