如果json有多个数据集,我该如何编写json模式

我是这个json架构的新手,如果它只有一个如下所示的数据集,我可以编写json架构

{ "employees": [ { "id": 1, "name": "aaa" } } 

示例json-schema用于此

 { "type" : "object", "required" : ["employees"], "properties" : { "employees" : { "type" : "Array", "items" : [ "properties" : { "id" : {"type" : "integer"}, "name" : {"type" : "string"}, }, "required" : ["id","name"] ] } } } 

但是如果我们有多个数据集,我就会坚持在ruby中编写json模式

 { "employees": [ { "id": 1, "name": "aaa" }, { "id": 2, "name": "bbb" }, { "id": 3, "name": "cccc" }, { "id": 4, "name": "ddd" }, { "id": 5, "name": "eeee" } ] } 

任何人都可以帮我写json架构,如果它有相同架构的多个数据集来validation响应体

这是您正在寻找的架构。

 { "type": "object", "required": ["employees"], "properties": { "employees": { "type": "array", "items": { "type": "object", "properties": { "id": { "type": "integer" }, "name": { "type": "string" } }, "required": ["id", "name"] } } } } 

你真的很亲密。 items关键字有两种forms。 items的值可以是模式或模式数组(1)。

如果items是模式,则意味着数组中的每个项都必须符合该模式。 这是在这种情况下有用的forms。

如果items的值是模式数组,则它描述一个元组。 例如,这个架构……

 { "type": "array", "items": [ { "type": "boolean" }, { "type": "string" } ] } 

会validation这个……

 [true, "foo"] 
  1. http://json-schema.org/latest/json-schema-validation.html#anchor37