Tho*_* V. 3 character-encoding special-characters dart flutter
我正在解析一个本地JSON文件,其中包含冰岛语中很少使用的特殊字符的单词。
显示字符时,我会混淆符号而不是字符,对于其他一些字符,我只会得到一个正方形而不是符号。
我正在使用这种类型的编码 "\u00c3"
更新:我使用的字符示例:þ, æ, ý, ð
问:显示这些字符并避免显示失败的最佳方法是什么?
更新 #2: 我是如何解析的:
Future<Null> getAll() async{
var response = await
DefaultAssetBundle.of(context).loadString('assets/json/dictionary.json');
var decodedData = json.decode(response);
setState(() {
for(Map word in decodedData){
mWordsList.add(Words.fromJson(word));
}
});
}
Run Code Online (Sandbox Code Playgroud)
班上:
class Words{
final int id;
final String wordEn, wordIsl;
Words({this.id, this.wordEn, this.wordIsl});
factory Words.fromJson(Map<String, dynamic> json){
return new Words(
id: json['wordId'],
wordEn: json['englishWord'],
wordIsl: json['icelandicWord']
);
}
}
Run Code Online (Sandbox Code Playgroud)
JSON 模型:
{
"wordId": 47,
"englishWord": "Age",
//Here's a String that has two special characters
"icelandicWord": "\u00c3\u00a6vi"
}
Run Code Online (Sandbox Code Playgroud)
小智 6
我对重音字符有类似的问题。它们没有按预期显示。这对我有用
final codeUnits = source.codeUnits;
return Utf8Decoder().convert(codeUnits);
Run Code Online (Sandbox Code Playgroud)
问题是您的 JSON 存储在本地。
\n\n假设你有
\nMap<String, String> jsonObject = {"info": "\xc3\x86 \xc3\xa6 \xc3\xa6 \xc3\x96 \xc3\xb6 \xc3\xb6"};
因此,要正确显示文本,您必须使用 utf-8 编码和解码 JSON。
\n\n我知道序列化和反序列化是成本高昂的操作,但它是本地存储的包含 UTF-8 文本的 JSON 对象的解决方法。
\n\nimport \'dart:convert\';\njsonDecode(jsonEncode(jsonObject))["info"]\nRun Code Online (Sandbox Code Playgroud)\n\n如果您从服务器获取 JSON,那么它会更简单,例如在dio包中您可以选择contentType默认为“application/json; charset=utf-8”的参数。
| 归档时间: |
|
| 查看次数: |
3656 次 |
| 最近记录: |