Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 需要JSON模式数组_Arrays_Json_Jsonschema - Fatal编程技术网

Arrays 需要JSON模式数组

Arrays 需要JSON模式数组,arrays,json,jsonschema,Arrays,Json,Jsonschema,我试图弄清楚如何设置模式中所需的全局级别数组。我的示例JSON文件是: [ { "firstname": "Paul", "lastname": "McCartney" }, { "firstname": "John", "lastname": "Lennon" }, { "firstname": "George", "lastname": "Harrison"

我试图弄清楚如何设置模式中所需的全局级别数组。我的示例JSON文件是:

[
    {
        "firstname": "Paul",
        "lastname": "McCartney"
    },
    {
        "firstname": "John",
        "lastname": "Lennon"
    },
    {
        "firstname": "George",
        "lastname": "Harrison"
    },
    {
        "firstname": "Ringo",
        "lastname": "Starr"
    }
]
如上所述,我希望顶层结构是一个数组,而不是一个对象。我从jsonschema.net获得的模式是(稍作修改):


但它失败了。您能帮我为顶级数组提供正确的JSON模式吗?

要对输入数据有效,您只需要以下模式:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "array",
    "id": "http://jsonschema.net",
    "items": {
        "type": "object",
        "properties": {
        "firstname": {
            "type": "string"
        },
        "lastname": {
            "type": "string"
        }
    },
    "required": [
        "firstname",
        "lastname"
    ]}
}

你不需要做最高级的事情。如果文档是数组,那么文档不是可选的!如果您试图指定最小项目数,请尝试
minItems
。@cloudfeet不知道,谢谢!嗯,我明白了-我定义了无用的顶级
required
字段。所需的数组元素级别
就足够了。太好了,谢谢!
{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "array",
    "id": "http://jsonschema.net",
    "items": {
        "type": "object",
        "properties": {
        "firstname": {
            "type": "string"
        },
        "lastname": {
            "type": "string"
        }
    },
    "required": [
        "firstname",
        "lastname"
    ]}
}