首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Dart/颤振json.decode与多行值

Dart/颤振json.decode与多行值
EN

Stack Overflow用户
提问于 2022-06-29 15:33:32
回答 1查看 101关注 0票数 0

我有多行的json数据。当我使用json.decode时,我会出错。

代码语言:javascript
运行
复制
"FormatException (FormatException: Control character in string 
... "count": "1", "price": "5", "description": "It is a long established fact

我的Json数据

代码语言:javascript
运行
复制
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."
}

谢谢,

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-29 15:53:54

不确定你是想编还是解码。

如果您希望编码(根据数据创建JSON字符串),您应该确保提供给json.encode的变量是Map<String, dynamic>类型的。此外,如果您想拥有多行字符串,则应该在Dart中使用三元引号"""。下面是一个例子

代码语言:javascript
运行
复制
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\"

代码语言:javascript
运行
复制
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);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72804149

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档