\ n在flutter中解析json时会出错

问题描述 投票:0回答:2

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

[
{
"variable1":"example",
"variable2": "example1\nexample2\nexample3"
},
{
"variable1":"exampleLast\n",
"variable2": "example1\nexample2\nexample3"
}
]

我试图将这个json解析为Flutter中的List。

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

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

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

android ios json dart flutter
2个回答
0
投票

你只需要替换单斜杠来加倍斜线,一切都很顺利。

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


0
投票

只需在代码中用双反斜杠(\\ n)替换单个反斜杠(\ n):

[{
    "variable1": "example",
    "variable2": "example1\\nexample2\\nexample3"
},
{
    "variable1": "exampleLast\\n",
    "variable2": "example1\\nexample2\\nexample3"
}]
© www.soinside.com 2019 - 2024. All rights reserved.