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

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

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


当前回答

\n\r\n为我工作!!

\n表示单行换行,\n\r\n表示双行换行

其他回答

您可以在客户端编码,在服务器端解码。这也可以处理\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库有支持多行字符串的选项。我是带着警告说的,这将损害您的互操作性。

然而,在我遇到的特定场景中,我需要使一个只被一个系统使用的配置文件可被人类阅读和管理。最终选择了这个解决方案。

下面是如何在Java上使用Jackson实现的:

JsonMapper mapper = JsonMapper.builder()
   .enable(JsonReadFeature.ALLOW_UNESCAPED_CONTROL_CHARS)
   .build()

\n\r\n为我工作!!

\n表示单行换行,\n\r\n表示双行换行

不幸的是,这里的许多答案都解决了如何在字符串数据中放入换行符的问题。问题是如何通过将字符串值拆分到多行代码中来使代码看起来更好。(即使是认识到这一点的答案也提供了假设人们可以自由改变数据表示的“解决方案”,而在许多情况下,人们并不是这样。)

更糟糕的是,没有好的答案。

在许多编程语言中,即使它们不显式地支持跨行分割字符串,您仍然可以使用字符串连接来获得所需的效果;只要编译器不是那么糟糕就可以了。

但是json不是一种编程语言;它只是一个数据表示。你不能告诉它去连接字符串。它的语法(相当小)也不包括任何在多行上表示字符串的工具。

除了设计某种类型的预处理器(就我个人而言,我不想有效地创造自己的语言来解决这个问题),这个问题没有一个通用的解决方案。如果可以更改数据格式,则可以替换字符串数组。否则,这就是json不是为人类可读性而设计的众多方面之一。

假设这个问题与轻松编辑文本文件,然后手动将它们转换为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.