我有多行的json数据。当我使用json.decode
时,我会出错。
"FormatException (FormatException: Control character in string
... "count": "1", "price": "5", "description": "It is a long established fact
我的Json数据
var str = {
"description": "It is a long established fact
that a reader will be distracted by the readable content
of a page when looking at its layout. The point
of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English."
}
谢谢,
发布于 2022-06-29 15:53:54
不确定你是想编还是解码。
如果您希望编码(根据数据创建JSON字符串),您应该确保提供给json.encode
的变量是Map<String, dynamic>
类型的。此外,如果您想拥有多行字符串,则应该在Dart中使用三元引号"""
。下面是一个例子
var data = {
"description": """It is a long established fact
that a reader will be distracted by the readable content
of a page when looking at its layout. The point
of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English."""
};
final jsonString = json.encode(data);
如果您想要解码(将JSON字符串转换为Dart对象),则输入字符串应该被正确格式化。JSON不支持字符串,因此您需要添加换行\n
,因此在字符串声明中也必须忽略这些字符串,就像JSON中的引号一样,导致了\\n
和\"
var str = "{\"description\": \"It is a long established fact\\nthat a reader will be distracted by the readable content\\nof a page when looking at its layout. The point\\nof using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English.\"}";
final data = json.decode(str);
https://stackoverflow.com/questions/72804149
复制相似问题