【发布时间】:2018-04-18 21:43:37
【问题描述】:
如何在以下 json 中检查数组 names 中的至少一个元素的属性 nickName 的值为 Ginny?
{
"names": [
{
"firstName": "Hermione",
"lastName": "Granger"
}, {
"firstName": "Harry",
"lastName": "Potter"
}, {
"firstName": "Ron",
"lastName": "Weasley"
}, {
"firstName": "Ginevra",
"lastName": "Weasley",
"nickName": "Ginny"
}
]
}
目前我使用的是 draft-06 版本(FAQ here)。
这是我的 NOT WORKING 架构:
{
"$schema": "http://json-schema.org/draft-06/schema#",
"title": "Complex Array",
"description": "Schema to validate the presence and value of an object within an array.",
"type": "object",
"properties": {
"names": {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"nickName": {
"type": "string"
}
},
"anyOf": [
{"required": ["nickName"]}
]
}
}
}
}
【问题讨论】:
-
您需要将 JSON 转换为字典格式,然后使用 dict["parameterName"] != nil 进行检查。
标签: json contains jsonschema