我有一个像这样的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
你只需要替换单斜杠来加倍斜线,一切都很顺利。
String replaced = string.replaceAll(r'\', r'\\');
只需在代码中用双反斜杠(\\ n)替换单个反斜杠(\ n):
[{
"variable1": "example",
"variable2": "example1\\nexample2\\nexample3"
},
{
"variable1": "exampleLast\\n",
"variable2": "example1\\nexample2\\nexample3"
}]