Python JSON架构验证失败-错误的类型不是';对象';
我试图使用jsonschema和python_jsonschema_对象库从模式文件创建python对象,在该对象中填充一些数据,然后根据原始模式验证它。不知何故,我认为我做错了什么,但不确定到底是什么 我尝试了几种不同的模式和数据值,并使用平面/单个对象删除了数组。但验证仍然失败Python JSON架构验证失败-错误的类型不是';对象';,python,json,jsonschema,Python,Json,Jsonschema,我试图使用jsonschema和python_jsonschema_对象库从模式文件创建python对象,在该对象中填充一些数据,然后根据原始模式验证它。不知何故,我认为我做错了什么,但不确定到底是什么 我尝试了几种不同的模式和数据值,并使用平面/单个对象删除了数组。但验证仍然失败 from jsonschema import validate import python_jsonschema_objects as pjs import jsonschema import json import
from jsonschema import validate
import python_jsonschema_objects as pjs
import jsonschema
import json
import os
with open('geocoordinate/geocoordinatearray3.schema.json') as opfile:
schema = json.load(opfile)
builder = pjs.ObjectBuilder(schema)
ns = builder.build_classes()
Coordinate = ns.Rootschema
ca = Coordinate(latitude=22.22,longitude=33.33)
print(ca.serialize())
try:
print("about to validate first example")
validate(instance=ca, schema=schema)
except jsonschema.exceptions. ValidationError as e:
print("this is validation error:", e)
except json.decorder.JSONDecodeError as e:
print("not JSON", e)
这是架构文件:
{
"definitions": {},
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://example.com/root.json",
"type": "object",
"title": "rootSchema",
"required": [
"latitude",
"longitude"
],
"properties": {
"location": {
"$id": "#/properties/location",
"type": "string",
"title": "The Location Schema",
"default": "",
"examples": [
"Denver, CO"
],
"pattern": "^(.*)$"
},
"latitude": {
"$id": "#/properties/latitude",
"type": "number",
"title": "The Latitude Schema",
"default": 0.0,
"examples": [
39.7392
]
},
"longitude": {
"$id": "#/properties/longitude",
"type": "number",
"title": "The Longitude Schema",
"default": 0.0,
"examples": [
-104.9903
]
},
"alt": {
"$id": "#/properties/alt",
"type": "integer",
"title": "The Alt Schema",
"default": 0,
"examples": [
5280
]
}
}
}
我希望这能得到验证,我想做的很简单。获取此错误:
即将验证第一个示例
这是验证错误:0>纬度=22.22>位置=>经度=33.33>>不是“对象”类型
验证架构中的“类型”失败:
模式
例如:
<rootschema alt=<Literal<int> 0> latitude=<Literal<float> 22.22>
location=<Literal<str> > longitude=<Literal<float> 33.33>>
我让它工作了,问题是打字。我必须获取python实例并序列化它。我认为默认情况下是字符串类型。谢谢大家的帮助 模式文件是您自己创建的,还是提供给您的?我使用了:我插入了一些示例数据并使用了它。我尝试了两种不同的模式,但总是出现相同的错误。我觉得我在python代码中做了一些错误的事情,但不确定是否向我们展示了用于验证模式的示例数据?我让它工作起来了,问题在于键入。我必须获取python实例并序列化它。我认为默认情况下是字符串类型。谢谢大家的帮助!