验证Json Schema

时间:2012-07-19 10:02:24

标签: json validation jsonschema

使用json-schema-validator API v4时出错。 我试着这样做:

final JsonValidator validator = new JsonValidator(JsonLoader.fromPath("schema.json"));
ValidationReport report = validator.validate(data);

但每次收到错误时:#[schema]:未知关键字联系人

schema.json :
{
    "contacts": {
        "description": "The list of contacts",
        "type": "array",
        "optional": true,
        "items": {
            "description": "A contact",
            "type": "object",
            "properties": {
                "givenName": {
                    "description": "Person's first name",
                    "type": "string",
                    "maxLength": 64,
                    "optional": true
                },
                "familyName": {
                    "description": "A person's last name",
                    "type": "string",
                    "maxLength": 64,
                    "optional": true
                }
            }
        }
    }
}

此致

1 个答案:

答案 0 :(得分:1)

据我所知,你的数据看起来像这样 - > json_data = {"触点":阵列}。如果这是真的,基本上你最外面的东西是一个对象(基本上是完整的json对象本身),你可以"可能"需要从"顶级根"开始定义架构。你的json as-> schema.json:

{
"description": "the outer json",
        "type": "object",
        "properties": {
            "contacts": {
                          "description": "The list of contacts",
                          "type": "array",
                          "optional": true,
                          "items": {
                                     "description": "A contact",
                                     "type": "object",
                                     "properties": {
                                     "givenName": {

etc.....

请原谅我粗略的缩进。另外,我没有对此进行测试,请查看它是否有效,如果没有,我建议您提供您的json_data(至少示例)和API的示例,以便人们可以尝试找到什么是错。