\n 在 flutter 中解析 json 时出错

Eni*_*aya 6 json dart flutter

我有一个像这样的 json 数组:

[
{
"variable1":"example",
"variable2": "example1\nexample2\nexample3"
},
{
"variable1":"exampleLast\n",
"variable2": "example1\nexample2\nexample3"
}
]
Run Code Online (Sandbox Code Playgroud)

我正在尝试在 Flutter 中解析这个 json 列表。

List posts = json.decode(response.data);

当我尝试使用 'dart:convert' 时,它给出了 error FormatException... Control character in string (at line...)

我在 Github 上发现了这个问题,但找不到解决方案。
https://github.com/dart-lang/convert/issues/10

Arj*_*yas 4

您只需要将单斜杠替换为双斜杠,一切都会顺利。

String replaced = string.replaceAll(r'\', r'\\');