Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 如何在flatter中动态显示json响应中的Unicode笑脸_Flutter_Unicode_Unicode String_Unicode Escapes - Fatal编程技术网

Flutter 如何在flatter中动态显示json响应中的Unicode笑脸

Flutter 如何在flatter中动态显示json响应中的Unicode笑脸,flutter,unicode,unicode-string,unicode-escapes,Flutter,Unicode,Unicode String,Unicode Escapes,如何在flatter中动态显示json响应中的Unicode笑脸。当我将字符串声明为静态时,它会正确显示,但从动态响应来看,它不会正确显示smiley 静态声明:(工作) 动态响应: "message":"\\ud83d\\ude4c Be Safe at your home \\ud83c\\udfe0", 当我解析并将此响应传递给文本时,它将Unicode视为字符串,并将其显示为字符串,而不是S笑Y代码,下面用S笑Y显示文本: child: Text

如何在flatter中动态显示json响应中的Unicode笑脸。当我将字符串声明为静态时,它会正确显示,但从动态响应来看,它不会正确显示smiley

静态声明:(工作)

动态响应:

"message":"\\ud83d\\ude4c Be Safe at your home \\ud83c\\udfe0",

当我解析并将此响应传递给文本时,它将Unicode视为字符串,并将其显示为字符串,而不是S笑Y代码,下面用S笑Y显示文本:

child: Text(_listData[index].message.toString().replaceAll("\\\\", "\\"))
已经讨论过了:但是它只在单个unicode不能与多个unicode一起工作时才起作用


任何使用unicode字符动态显示文本的人,请告诉我。

问题通过使用下面的代码片段解决

  Client client = Client();
  final response = await client.get(Uri.parse('YOUR_API_URL'));
  if (response.statusCode == 200) {
    // If the server did return a 200 OK response,
    // then parse the JSON.
    final extractedData = json.decode(response.body.replaceAll("\\\\", "\\"));
        }
在这里,我们需要将双反斜杠替换为单反斜杠,然后在设置为文本之前解码JSON响应,我们可以显示多个unicode,如下所示:

final extractedData=json.decode(response.body.replaceAll(“\\”), "\"));


希望这个答案能对其他人有所帮助

另一个我想给unescape角色的好解决方案是: 第一->

这将打印并无法转义字符:

\ud83d\ud83d Be Safe at your home \ud83c\ud83d
以上就是输出

因此,我们可以在解析时取消对它们的扫描,或者使用:

String convertStringToUnicode(String content) {
  String regex = "\\u";
  int offset = content.indexOf(regex) + regex.length;
  while(offset > 1){
    int limit = offset + 4;
    String str = content.substring(offset, limit);
//     print(str);
    if(str!=null && str.isNotEmpty){
      String uni = String.fromCharCode(int.parse(str,radix:16));
 
   
      content = content.replaceFirst(regex+str,uni);
//       print(content);
      
    }
    offset = content.indexOf(regex) + regex.length;
//     print(offset);
  }
  return content;
  
}
这将替换所有文字并将其转换为unicode字符以及表情符号的结果和输出:

String k=convertStringToUnicode(q);
印刷品(k);

要显示的字符串实际上根本不包含文字反斜杠<代码>\u1234
是一个字符,其代码点为(十六进制)1234。如果后端以转义形式生成这些,则代码需要取消转义。如何做到这一点是一个常见的常见问题。嘿@tripleee感谢好友的问题由您给出的提示解决。我没有尝试,但可能对其他人有帮助。谢谢
\ud83d
是一个高级代理,因此
\ud83d\ud83d
什么都不是。请使用有效的代理项对尝试您的代码更新您的答案,例如
\ud83d\ude0e
,这应该会让@JosefZ非常感谢您的提及。我已经更新了我的答案,它对这两种情况都很有效。
\ud83d\ud83d Be Safe at your home \ud83c\ud83d
String convertStringToUnicode(String content) {
  String regex = "\\u";
  int offset = content.indexOf(regex) + regex.length;
  while(offset > 1){
    int limit = offset + 4;
    String str = content.substring(offset, limit);
//     print(str);
    if(str!=null && str.isNotEmpty){
      String uni = String.fromCharCode(int.parse(str,radix:16));
 
   
      content = content.replaceFirst(regex+str,uni);
//       print(content);
      
    }
    offset = content.indexOf(regex) + regex.length;
//     print(offset);
  }
  return content;
  
}