是否有可能在JSON中有多行字符串?

这主要是为了视觉上的舒适,所以我想我可以在编辑器中打开自动换行,但我只是有点好奇。

我正在编写JSON格式的一些数据文件,并希望有一些非常长的字符串值分割在多行。使用python的JSON模块,无论我使用\或\n作为转义,我都会得到很多错误。


当前回答

假设这个问题与轻松编辑文本文件,然后手动将它们转换为json有关,我发现了两个解决方案:

hjson (that was mentioned in this previous answer), in which case you can convert your existing json file to hjson format by executing hjson source.json > target.hjson, edit in your favorite editor, and convert back to json hjson -j target.hjson > source.json. You can download the binary here or use the online conversion here. jsonnet, which does the same, but with a slightly different format (single and double quoted strings are simply allowed to span multiple lines). Conveniently, the homepage has editable input fields so you can simply insert your multiple line json/jsonnet files there and they will be converted online to standard json immediately. Note that jsonnet supports much more goodies for templating json files, so it may be useful to look into, depending on your needs.

其他回答

您可以在客户端编码,在服务器端解码。这也可以处理\n和\t字符

例:我需要通过json发送多行xml

{
  "xml": "PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiID8+CiAgPFN0cnVjdHVyZXM+CiAgICAgICA8aW5wdXRzPgogICAgICAgICAgICAgICAjIFRoaXMgcHJvZ3JhbSBhZGRzIHR3byBudW1iZXJzCgogICAgICAgICAgICAgICBudW0xID0gMS41CiAgICAgICAgICAgICAgIG51bTIgPSA2LjMKCiAgICAgICAgICAgICAgICMgQWRkIHR3byBudW1iZXJzCiAgICAgICAgICAgICAgIHN1bSA9IG51bTEgKyBudW0yCgogICAgICAgICAgICAgICAjIERpc3BsYXkgdGhlIHN1bQogICAgICAgICAgICAgICBwcmludCgnVGhlIHN1bSBvZiB7MH0gYW5kIHsxfSBpcyB7Mn0nLmZvcm1hdChudW0xLCBudW0yLCBzdW0pKQogICAgICAgPC9pbnB1dHM+CiAgPC9TdHJ1Y3R1cmVzPg=="
}

然后在服务器端解码

public class XMLInput
{
        public string xml { get; set; }
        public string DecodeBase64()
        {
            var valueBytes = System.Convert.FromBase64String(this.xml);
            return Encoding.UTF8.GetString(valueBytes);
        }
}

public async Task<string> PublishXMLAsync([FromBody] XMLInput xmlInput)
{
     string data = xmlInput.DecodeBase64();
}

一旦解码,您将得到原始的XML

<?xml version="1.0" encoding="utf-8" ?>
  <Structures>
       <inputs>
               # This program adds two numbers

               num1 = 1.5
               num2 = 6.3

               # Add two numbers
               sum = num1 + num2

               # Display the sum
               print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))
       </inputs>
  </Structures>

假设这个问题与轻松编辑文本文件,然后手动将它们转换为json有关,我发现了两个解决方案:

hjson (that was mentioned in this previous answer), in which case you can convert your existing json file to hjson format by executing hjson source.json > target.hjson, edit in your favorite editor, and convert back to json hjson -j target.hjson > source.json. You can download the binary here or use the online conversion here. jsonnet, which does the same, but with a slightly different format (single and double quoted strings are simply allowed to span multiple lines). Conveniently, the homepage has editable input fields so you can simply insert your multiple line json/jsonnet files there and they will be converted online to standard json immediately. Note that jsonnet supports much more goodies for templating json files, so it may be useful to look into, depending on your needs.

查看规格!JSON语法的char生成可以取以下值:

any-Unicode-character-except——“或者——\ -or-control-character \” \ \ \/ \ b \ f \ n r \ \ t \ u four-hex-digits

换行符是“控制字符”,所以,不,你可以在你的字符串中没有文字换行符。然而,你可以使用你需要的\n和\r的任意组合来编码它。

JSON不允许真正的换行。您需要将所有换行符替换为\n。

eg:

"first line
second line"

可以通过以下方式保存:

“一线/二线”

注意:

对于Python,这应该写成:

“一线、二线”

\\是用来转义反斜杠的,否则python会把\n当作 控制字符“new line”

JSON不允许为了可读性而换行。

最好的办法是使用一个可以为您换行的IDE。