2012-07-19 102 views
1

使用json-schema-validator API v4时出现错误。 我尝试做:验证Json Schema

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

但每次我得到一个错误的时间:#[方案]:未知的关键字的联系人

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

据我可以直觉,你的数据看起来像这样 - > json_data = {“contacts”:array}。如果这是真的,基本上你最外面的东西是一个对象(基本上是完整的json对象本身),你可能需要从你的json的“顶级根目录”开始定义schema-> 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的例子,以便可以找出哪里出错。