【问题标题】:Is it problematic to have a JSON Lines file that has mixed JSON structures?拥有混合 JSON 结构的 JSON Lines 文件是否有问题?
【发布时间】:2016-04-07 20:52:34
【问题描述】:

我想知道如果 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"}

它是否被视为无效的 JSONLines 格式?我查看了http://jsonlines.org/ 的标准,但无论如何我都看不到任何东西。 谢谢。

【问题讨论】:

    标签: json jsonlines


    【解决方案1】:

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

    数组示例:

    [
     {"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/

    【讨论】:

    • 谢谢!!!我错过了大括号,因为我只是快速尝试创建虚拟数据来说明这一点,但您已经回答了关于将混合结构放在一个文件中的问题:)
    猜你喜欢
    • 2011-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多