我正在尝试输出将被我的架构中的以下结构接受的 JSON。架构的相关部分在这里:
"special_needs": {
"type": "object",
"input-type":"checkbox",
"label": "learning differences",
"help-block":"please check all that apply",
"anyOf": {
"category_0": {
"main": { "enum": ["learning"]},
"sub": { "enum": ["ADD/ADHD", "dyslexia", "general learning disability", "language disorder", "intellectual giftedness", "other"]}
},
"category_1": {
"main": { "enum": ["mental"]},
"sub": { "enum": ["down's syndrome", "asperger's syndrome", "autism", "other"]}
},
"category_2": {
"main": { "enum": ["behavioral"]}
},
"category_3": {
"main": { "enum": ["medical"]},
"sub": { "enum": ["diabetes", "allergies", "eating disorders", "chronic illness", "other"]}
},
"category_4": {
"main": { "enum": ["physical"]},
"sub": { "enum": ["blind", "deaf", "cerebral palsy", "other"]}
}
}
},
我尝试编写将使用以下内容接受的 JSON,它应该默认为空数组,因此被验证器接受。
main = row.get('main', [])
sub = row.get('sub',[])
special_needs = dict(main = main, sub = sub)
但是得到这个错误:
Failed validating u'type' in schema[u'properties'][u'definitions'][u'additionalProperties'][u'properties'][u'properties'][u'additionalProperties'][u'properties'][u'anyOf']:
{u'items': {u'$ref': u'#'}, u'minItems': 1, u'type': u'array'}
File "/Library/Python/2.7/site-packages/jsonschema/validators.py", line 76, in check_schema
raise SchemaError.create_from(error)
jsonschema.exceptions.SchemaError: {'category_4': {'main': {'enum': ['physical']}, 'sub': {'enum': ['blind', 'deaf', 'cerebral palsy', 'other']}}, 'category_3': {'main': {'enum': ['medical']}, 'sub': {'enum': ['diabetes', 'allergies', 'eating disorders', 'chronic illness', 'other']}}, 'category_2': {'main': {'enum': ['behavioral']}}, 'category_1': {'main': {'enum': ['mental']}, 'sub': {'enum': ["down's syndrome", "asperger's syndrome", 'autism', 'other']}}, 'category_0': {'main': {'enum': ['learning']}, 'sub': {'enum': ['ADD/ADHD', 'dyslexia', 'general learning disability', 'language disorder', 'intellectual giftedness', 'other']}}} is not of type u'array'
On instance[u'definitions']['person'][u'properties']['special_needs'][u'anyOf']:
{'category_0': {'main': {'enum': ['learning']},
'sub': {'enum': ['ADD/ADHD',
'dyslexia',
'general learning disability',
'language disorder',
'intellectual giftedness',
'other']}},
'category_1': {'main': {'enum': ['mental']},
'sub': {'enum': ["down's syndrome",
"asperger's syndrome",
'autism',
'other']}},
'category_2': {'main': {'enum': ['behavioral']}},
'category_3': {'main': {'enum': ['medical']},
'sub': {'enum': ['diabetes',
'allergies',
'eating disorders',
'chronic illness',
'other']}},
'category_4': {'main': {'enum': ['physical']},
'sub': {'enum': ['blind',
'deaf',
'cerebral palsy',
'other']}}}
logout
为了获得正确的 JSON,我应该写什么?
新代码:
main = row.get('main', [])
sub = row.get('sub',[])
category_0 = dict(main = main, sub = sub)
category_1 = dict(main = main, sub = sub)
category_2 = dict(main = main, sub = sub)
category_3 = dict(main = main, sub = sub)
category_4 = dict(main = main, sub = sub)
special_needs = dict(category_0 = category_0, category_1 = category_1, category_2 = category_2, category_3 = category_3, category_4 = category_4)
新输出:
'special_needs': {'category_0': {'main': [], 'sub': []},
'category_1': {'main': [], 'sub': []},
'category_2': {'main': [], 'sub': []},
'category_3': {'main': [], 'sub': []},
'category_4': {'main': [], 'sub': []}},