从 HTTP 响应解码字符串时,我得到 FormatException。这是因为字符串中的 \n 字符。当我将字符串转换为原始字符串时,它会起作用。
声明一个原始字符串很容易
String raw = r'Hello \n World';
Run Code Online (Sandbox Code Playgroud)
但是如何将现有字符串转换为原始字符串?
String notRaw = 'Hello \n World';
String raw = r'${notRaw}';
Run Code Online (Sandbox Code Playgroud)
上面的语句不起作用,因为 r' 被视为原始字符串之后的所有内容。
我有两个问题
1) 如何在解码 JSON 时避免 \n 问题。2) 如何将现有的字符串变量转换为原始字符串。
import 'dart:convert';
void main() {
var jsonRes = """
{
"response-list": {
"response": [
{
"attribute": {
"@name": "Problem",
"@isEditable": false,
"@value": "Services fail to respond; for example:\n\n1) unable to connect.\n2) Slow response on the console.\n3)no response."
}
}
]
}
}
""";
var jsonStr …Run Code Online (Sandbox Code Playgroud)