我正在使用一个JSON模式,并尝试使用pythonjson模块来验证我根据该模式输出的一些JSON。在
我得到以下错误,表明架构本身没有验证:
validation
Traceback (most recent call last):
File "/private/var/folders/jv/9_sy0bn10mbdft1bk9t14qz40000gn/T/Cleanup At Startup/gc_aicep-395698294.764.py", line 814, in <module>
validate(entry,gc_schema)
File "/Library/Python/2.7/site-packages/jsonschema/validators.py", line 468, in validate
cls(schema, *args, **kwargs).validate(instance)
File "/Library/Python/2.7/site-packages/jsonschema/validators.py", line 117, in validate
raise error
jsonschema.exceptions.ValidationError: ({'website': 'www.stepUp.com', 'bio': '', 'accreditations': {'portfolio': '', 'certifications': [], 'degrees': {'degree_name': [], 'major': '', 'institution_name': '', 'graduation_distinction': '', 'institution_website': ''}}, 'description': 'A great counselor', 'photo': '', 'twitter': '', 'additionaltext': '', 'linkedin': '', 'price': {'costtype': [], 'costrange': []}, 'phone': {'phonetype': [], 'value': '1234567891'}, 'facebook': '', 'counselingtype': [], 'logourl': '', 'counselingoptions': [], 'linkurl': '', 'name': {'first_name': u'Rob', 'last_name': u'Er', 'middle_name': u'', 'title': u''}, 'email': {'emailtype': [], 'value': ''}, 'languages': 'english', 'datasource': {'additionaltext': '', 'linkurl': '', 'linktext': '', 'logourl': ''}, 'linktext': '', 'special_needs_offer': '', 'company': 'Step Up', 'location': {'city': 'New York', 'zip': '10011', 'locationtype': '', 'state': 'NY', 'address': '123 Road Dr', 'loc_name': '', 'country': 'united states', 'geo': ['', '']}},) is not of type 'object'
validationError消息表明冒号后面的对象不是一个有效的JSON对象,我想,但是我不明白为什么它不是。在
如果将单引号替换为双引号并从两边删除基本括号,则此JSON将使用JSON Lint之类的验证器进行验证。在
名称前的“u”已标记为可能的错误。在
这是输出名称的代码:
^{pr2}$使用以下命令将名称插入到JSON中:
gc_ieca = dict(
name = full_name,
twitter = twitter,
logourl = logourl,
linktext = linktext,
linkurl = linkurl,
additionaltext = additionaltext,
datasource = datasource,
phone=phone,
email = email,
price = price,
languages = languages,
special_needs_offer = special_needs_offer,
# location
location = location,
accreditations = accreditations,
website = website
),
这不是
ValidationError
所表示的。它表示验证失败:),并不是说JSON是无效的(jsonschema
甚至不处理JSON,而是处理反序列化的JSON,即Python对象,这里是dict)。如果JSON无效,则在调用json.load
时会出现错误。在它失败的原因是因为事实上它不是一个对象,它是一个只有一个元素的元组,一个对象,所以它实际上是无效的。为什么它是元组是你代码中的一个错误(我看到你的末尾有一个离群的逗号)。在
(另外,请注意,
u
前缀是因为它们是unicode文本,单引号是因为这是str
的repr
,与JSON无关)。在我在这里看到了两个潜在的问题:
u'...'
,这不是有效的json。使用u
必须后跟4个十六进制数字,并且应该在\
转义字符之后的字符串周围的双引号内。在相关问题 更多 >
编程相关推荐