我使用了jsonschme验证器来验证针对json文件的Json输出。
from jsonschema import validate #https://pypi.python.org/pypi/jsonschema
def assertDataMatchesSchema(self, data, schema_file_name):
with open(os.path.join("mha/resource_jsonschema", schema_file_name)) as schema_file:
validate(data, json.load(schema_file))以下是我的jsonschema:
{ "code": {"type":["string","null"]},
"codingMethod": {"type":["string","null"]},
"priority":{"type":["string","null"]},
"status":{"type":["string","null"]} ,
"description" : {"type" : "string"}
}终端输出:
SchemaError: {u'type': u'string'} is not of type u'string'
Failed validating u'type' in schema[u'properties'][u'description']:
{u'type': u'string'}
On instance[u'description']:
{u'type': u'string'}问题:如果我从上面的文件中删除了 description 字段,或者更改为其他名称,那么它可以工作,但是我需要描述字段(必需的nne)。有解决这个问题的办法吗?
如果我在那里使用"type"字段,同样的问题。
发布于 2013-09-11 11:37:31
描述是json模式使用的关键。所以你的模式应该是:-
schema = {
"type": "object",
"properties": {
"code": {"type":["string","null"]},
"codingMethod": {"type":["string","null"]},
"priority":{"type":["string","null"]},
"status":{"type":["string","null"]} ,
"description" : {"type" : "string"}
}
}
data = {"description" : "nowtry"}
validate(data, schema)对我来说很管用..。
您可以在这里看到您的模式应该如何,http://www.w3resource.com/JSON/JSON-Schema.php
https://stackoverflow.com/questions/18739558
复制相似问题