2016-04-07 25 views
0

我想知道是否,如果一个JSON行文件的结构是这样的:拥有混合了JSON结构的JSON行文件有问题吗?

{"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"} 

是它视为非有效JSONLines格式?我看着http://jsonlines.org/的标准,我看不到任何其他的东西。 谢谢。

回答

0

你的每一行都是有效的(但是缺少'}'和第二和第三的结尾)。 但如果你想把它们放在一个文件中,它将是无效的。他们必须在阵列并用,或与键/值的对象(其中值可以是数组或对象)阵列的

例如分离:

[ 
{"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/http://jsonlint.com/

+0

谢谢!我错过了大括号,因为我只是很快尝试创建虚拟数据来说明这一点,但您已经回答了关于将混合结构放在一个文件中的问题:) – rstruck