我试图将一些Markdown文本发送到休息api。刚才我发现json不接受断行。
实施例。如何将此发送到我的api:
An h1 header
============
Paragraphs are separated by a blank line.
2nd paragraph. *Italic*, **bold**, and `monospace`. Itemized lists
look like:
* this one
* that one
* the other one
Note that --- not considering the asterisk --- the actual text
content starts at 4-columns in.
> Block quotes are
> written like so.
>
> They can span multiple paragraphs,
> if you like.
Use 3 dashes for an em-dash. Use 2 dashes for ranges (ex., "it's all
in chapters 12--14"). Three dots ... will be converted to an ellipsis.
Unicode is supported. ☺
作为
{
"body" : " (the markdown) ",
}
答案 0 :(得分:1)
当您尝试将其发送到REST API端点时,我假设您正在寻找使用Javascript进行此操作的方法(因为您没有指定什么技术)正在使用)。
经验法则:除非您的目标是重新构建JSON构建器,否则请使用已存在的构建器。
而且,猜猜看,Javascript实现了它的JSON工具! (see documentation here)
如the documentation中所示,您可以使用JSON.stringify函数将对象(如字符串)转换为符合json的编码字符串,以后可以在服务器上进行解码侧。
此示例说明了如何执行此操作:
var arr = {
text: "This is some text"
};
var json_string = JSON.stringify(arr);
// Result is:
// "{"text":"This is some text"}"
// Now the json_string contains a json-compliant encoded string.
您还可以使用其他JSON.parse()
方法(see documentation)使用javascript解码JSON客户端:
var json_string = '{"text":"This is some text"}';
var arr = JSON.parse(json_string);
// Now the arr contains an array containing the value
// "This is some text" accessible with the key "text"
如果您没有回答您的问题,请对其进行编辑以使其更加精确,尤其是您正在使用的技术。我会相应地编辑这个答案
答案 1 :(得分:0)
您需要将行尾替换为\n
,然后将其传递给body
键。
此外,请确保您用"
括住双引号( \"
),否则您的身体将在此处结束。
# An h1 header\n============\n\nParagraphs are separated by a blank line.\n\n2nd paragraph. *Italic*, **bold**, and `monospace`. Itemized lists\nlook like:\n\n * this one\n * that one\n * the other one\n\nNote that --- not considering the asterisk --- the actual text\ncontent starts at 4-columns in.\n\n> Block quotes are\n> written like so.\n>\n> They can span multiple paragraphs,\n> if you like.\n\nUse 3 dashes for an em-dash. Use 2 dashes for ranges (ex., \"it's all\nin chapters 12--14\"). Three dots ... will be converted to an ellipsis.\nUnicode is supported.