我使用 jsonschema 模块在python中编写了JSON验证器。 它没有正确验证架构。我还使用基于网络的工具 http://jsonschemalint.com/ 进行验证。
我想要完全相似的东西。我把我的代码放在这里,请指出我正在酝酿的事情。
from jsonschema import validate
import json
class jsonSchemaValidator(object):
def __init__(self, schema_file):
self.__schema_file = open(schema_file)
self.__json_schema_obj = json.load(self.__schema_file)
def validate(self, json_file):
json_data_obj = json.load(open(json_file))
try:
validate(json_data_obj, self.__json_schema_obj)
print 'The JSON is follows the schema'
except Exception, extraInfo:
print str(extraInfo)
data_file_path = 'C:\\Users\\LT-BPant\\Desktop\\Del\\Schema\\new schema\\sample_output\\'
schema_path = 'C:\\Users\\LT-BPant\\Desktop\\Del\\Schema\\new schema\\'
def main():
json_file = data_file_path + 'report.json'
schema = schema_path+ 'report_new.schema'
obj = jsonSchemaValidator(schema)
obj.validate(json_file)
main()
我手动修改了json数据,但我仍然以JSON DATA follows the schema
作为输出,而基于网络的工具正确地显示了差异。