多行字符串的JSON和模板文字

问题描述 投票:5回答:2

我想在JSON文件中存储一些sql字符串。这就是JSON文件在我的理想世界中的样子

[
  "select t.index1, t.title, t.description, t.insertDate
  from myTable t
  join anotherTable t2 on t.index1 = t2.fIndex
  where t2.active = 1",
  ...
]

明显的限制是JSON和javascript不支持多行字符串。

我想避免这样做:

[
  "select t.index1, t.title, t.description, t.insertDate\nfrom myTable t\njoin anotherTable t2 on t.index1 = t2.fIndex\nwhere t2.active = 1",
  ...
]    

我看到一个人这样做了,这也不理想:

[
  ["select t.index1, t.title, t.description, t.insertDate",
  "from myTable t",
  "join anotherTable t2 on t.index1 = t2.fIndex",
  "where t2.active = 1"],
  ...
]

然后在他的翻译程序中他使用了myMultilineStringInArrayForm.join("\n")

我想知道是否有人为此目的有任何关于模板字符串工作(或计划在将来工作)的信息,如下所示:

[
  `select t.index1, t.title, t.description, t.insertDate
  from myTable t
  join anotherTable t2 on t.index1 = t2.fIndex
  where t2.active = 1`,
  ...
]

注意我在ES6的模板文字中使用了反引号(`),显然变量的插值对于存储的JSON不起作用,但是这个文字的多行特征是我感兴趣的。

请注意,这个问题类似于这个6岁的问题:Multiline strings in JSON

我再次问,因为我想知道`语法是否已经计划好或者已经有效了。

我很感激这方面的帮助

json ecmascript-6 multiline template-literals
2个回答
0
投票

看起来我拥有可读存储格式的最佳选择是XML。我试图远离XML并且认为由于`语法,JSON在未来会有多行字符串,但是没有


0
投票

你可以使用支持反引号的JSON6

这似乎是一个小的单人项目,所以也许更好的支持JSON5支持通过转换新行字符跨越多行的字符串可能是一个更好的选择。

© www.soinside.com 2019 - 2024. All rights reserved.