简体   繁体   English

JSON是否接受多行数组?

[英]Does JSON accept multi-line arrays?

I am trying to build a SPA ( Single Page Application ) as a simple website using only JSON, while I am learning JavaScript. 我在学习JavaScript的同时,尝试仅使用JSON将SPA(单页应用程序)构建为一个简单的网站。

In the technique I use, page content with HTML tags are injected by JavaScript. 在我使用的技术中,带有HTML标签的页面内容是由JavaScript注入的。

Since last JavaScript versions can store data between backticks 由于最新的JavaScript版本可以在反引号之间存储数据

`
Multi
Line 
Data
`

I am asking if multi-line JSON objects can store data by the same technique or something similar, so I can implement a way to edit data, without importing each tag from a HTML file, as new JavaScript framework do. 我问多行JSON对象是否可以通过相同的技术或类似方法存储数据,因此我可以实现一种编辑数据的方法,而不必像新的JavaScript框架那样从HTML文件导入每个标签。

`` "" '' 

For example: 例如:

var spa = {
..."spa_html_id_body_tag":"< h2 > body content < h2/ > < br > Bal bla bla .............."
}

JSON doesn't support backticks like JavaScript template literals do. JSON不像JavaScript模板文字那样支持反引号。 But it shouldn't be a problem. 但这不应该是一个问题。 You can still create the multi-line string using a template literal, and when you call JSON.stringify() it will use \\n in the JSON for the newlines. 您仍然可以使用模板文字来创建多行字符串,并且当您调用JSON.stringify() ,它将在JSON中使用\\n作为换行符。

JSON is an extremely rigid schema. JSON是一个非常严格的模式。 It's great, but it has a couple of shortcomings, the largest of which is the inability to store multi-line strings. 很好,但是它有一些缺点,其中最大的缺点是无法存储多行字符串。 This can be particularly annoying for storing things like structured text and public keys in JSON for later interaction with JavaScript code in the browser or on the server in Node. 对于将诸如结构化文本和公共密钥之类的内容存储在JSON中,以便以后在浏览器中或在Node中的服务器上与JavaScript代码进行交互时,这尤其令人讨厌。

Fortunately, there is a quick and hacky solution! 幸运的是,这里有一个快速而简单的解决方案!

Example Imagine that we have a list of servers and their associated public keys. 示例假设我们有一个服务器及其关联的公共密钥的列表。 To store that data as JSON, we could store it as an array of comma-separated string objects, which might look something like this: 要将数据存储为JSON,我们可以将其存储为以逗号分隔的字符串对象数组,该数组可能看起来像这样:

{
    "servers": {
        "servername.com": {
            "publicKey": [
                "-----BEGIN PUBLIC KEY-----",
                "MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEA0VmDBbzXdgubV/X8JP9B",
                "rM/CqvXBey49BoYjk0ar4OA4LZnmD0A/pn6voPPe+DQC3ZpSFfkdZc5s/ij7edve",
                "ob+reWdGOQAQnDxaGQoFyru/2tz1lNfaVOBU71QcpG4kda4XDjbS62hOqebgkQ0X",
                "71ireglZeaJABvu6EFPFBwyr2ROmM1dq4rKtinpgUf9CTMPL/8yyitka+HsVZTuI",
                "HDpFFxv6xPntfjMrIIRUPieIjRSJcF9yrTVqDIIOO9J3KplphXLXhAHRAnX3ducD",
                "sZYe5yZZEdkS9Kx4P0INkyiqs9DFGwVnIMU07IuM1E+LqLVTqm46MVxzrjaAcyVC",
                "WDTtzkERgvvaG8elbjb6iiA9aijNHM/EK0f68mjme7s0CHn9GJg4TwwvSLb8LqCs",
                "cCKT11WAanE+SuAi0WinuaeFORwAFFXh7O5sS8At4woCNVsIZpFSWWiRBoZf5UBC",
                "SfK8v9AL58foDDiCGlWKcpoQN5KBDBCTBD+RiXgoCKS9daHGpmXfAFsWmcjpVzvs",
                "l24ei4Ue1jd7kj7Dlc8qtXQGZS1BhwDaIV4SQBGYKEBgTMExcsAtI9Sd1rtMFybO",
                "wdfGmZkQ7pcLqOgEzyUOma+vwhWlvtfQXavH5NEdAS+ahsf8SDAI0TQtihLxTvO5",
                "LLrORy0wclzv7V0s+6qoRuMCAwEAAQ==",
                "-----END PUBLIC KEY-----"
            ]
        }
    }
}

You can verify that this valid JSON with JSONLint. 您可以使用JSONLint验证此有效JSON

Then, to retrieve our text block, we just have to join them back together on the newline characters, which we can do like this: 然后,要检索我们的文本块,我们只需要将它们重新连接到换行符上即可,我们可以这样做:

var publicKey = our_data.servers['servername.com'].publicKey.join('\n');

And that's it! 就是这样! Now you can store multiline strings in JSON. 现在,您可以在JSON中存储多行字符串。 It's not the prettiest thing, but it is the simplest given the constraints of JSON. 这不是最漂亮的东西,但是鉴于JSON的限制,这是最简单的事情。

By definition, JSON data uses strings enclosed by double quotation marks. 根据定义,JSON数据使用双引号引起来的字符串。 Depending on your use-case, using HTML tags may still be the most simple solution: 根据您的用例,使用HTML标记仍可能是最简单的解决方案:

{
    "text": "Multi<br />Line<br />Data"
}

If you'd prefer using the \\n notation to represent a new line, you could then write the text as "Multi\\nLine\\nData" and - if necessary - use the replace method when you import it into your JavaScript: 如果您希望使用\\n表示法来表示新行,则可以将文本编写为"Multi\\nLine\\nData"并且在必要时在将其导入JavaScript时使用replace方法:

JSON.parse(json).text.replace(/\n/g, '<br />');

But if you want greater flexibility than that, then you'll probably need to enter the world of HTML templating. 但是,如果您想要更大的灵活性,那么您可能需要进入HTML模板世界。 For an option that's simpler than HTML, I'd recommend taking a look at Markdown . 对于比HTML更简单的选项,我建议您看一下Markdown In Markdown, your example could simply be written: 在Markdown中,您的示例可以简单地编写为:

Multi
Line
Data

You can reference the Markdown file in your JSON, and use that reference to import it into your JavaScript. 您可以在JSON中引用Markdown文件,并使用该引用将其导入JavaScript。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM