具有混合 JSON 结构的 JSON 行文件是否有问题?

Is it problematic to have a JSON Lines file that has mixed JSON structures?

我想知道,如果 JSON Lines 文件的结构是这样的:

{"structure1":"some kind of file header"}
{"structure2": [ {"key1":"aaa", "key2":"bbb"}, {"key1":"one", "key2":"two"}]
{"structure2": [ {"key1":"xxx", "key2":"yyy"}, {"key1":"first", "key2":"second"}]
{"structure3":"maybe some kind of file footer"}

它是否被认为是无效的 JSON行格式?我查看了 http://jsonlines.org/ 上的标准,但我看不到任何东西。 谢谢。

您的每一行都有效(但缺少“}”以及第二行和第三行的结尾)。 但是如果你想把它们全部放在一个文件中,那将是无效的。它们必须在数组中并用 , 分隔或用 key/value 分隔的对象(其中值可以是数组或对象)

数组示例:

[
 {"structure1":"some kind of file header"},
 {"structure2": [ {"key1":"aaa", "key2":"bbb"}, {"key1":"one", "key2":"two"}]},
 {"structure2": [ {"key1":"xxx", "key2":"yyy"}, {"key1":"first", "key2":"second"}]},
 {"structure3":"maybe some kind of file footer"}
]

或对象:

{
"structure1": "some kind of file header",
"structure2": [{
    "key1": "aaa",
    "key2": "bbb"
}, {
    "key1": "one",
    "key2": "two"
}],
"structure2": [{
    "key1": "xxx",
    "key2": "yyy"
}, {
    "key1": "first",
    "key2": "second"
}],
"structure3": "maybe some kind of file footer"

}

您可以在此站点上测试您的 json 是否有效: http://www.jslint.com/ or http://jsonlint.com/