飞镖/颤振json.decode和多行值



我有json数据与多行。当我使用json.decode时,我有一个错误。

"FormatException (FormatException: Control character in string 
... "count": "1", "price": "5", "description": "It is a long established fact

My Json Data

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."
}

谢谢,

不确定是要编码还是解码

如果你想编码(从数据中创建一个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);

最新更新