我最近问过this问题,该如何删除列表中的名称,但是我意识到我真正想做的就是摆脱数组中字典的名称。
我想验证这样的结构(注意,字典未命名):
{
"some list": [
{
"foo": "bar"
},
{
"bin": "baz"
}
]
}
答案 0 :(得分:2)
问题似乎是试图将数组描述为“具有未命名属性的对象”的模棱两可。
如果您在两者之间遗漏了不必要的对象,则会得到一个简单的模式:
{
"type": "object",
"properties": {
"some list": {
"type": "array",
"items": {
"anyOf": [
{
"type": "string",
"description": "a string"
},
{
"type": "integer",
"minimum": 0,
"description": "Default time to expose a single image layer for."
}
]
}
}
}
}
答案 1 :(得分:0)
我尝试了这个。不要奇怪我有另一个问题的文件。
[ {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
52.743356,
-111.546907
]
},
"properties": {
"dealerName": "Smiths Equipment Sales (Smiths Hauling)",
"address": "HWY 13 - 5018 Alberta Avenue",
"city": "Lougheed",
"state": "AB",
"zip": "T0B 2V0",
"country": "Canada",
"website": "http://smithsequipsales.com/",
"phone": "780-386-3842",
"dealerCode": "SMI06",
"tractor": true,
"utv": false,
"hp": false
} }, {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [36.16876,-84.07945]
},
"properties": {
"dealerName": " Tommy's Motorsports",
"address": "2401 N Charles G Seivers Blvd",
"city": "Clinton",
"state": "TN",
"statename": "United States",
"zip": "37716",
"phone": "865-494-6500",
"website": "https://www.tommysmotorsports.com/",
"dealerCode": "TOM08",
"tractor": true,
"utv": false,
"hp": false
}
}
]
您可以这样输入:
import json
data = json.load(open('test.json'))
for i in data:
print(i["type"])
答案 2 :(得分:0)
JSON模式替代(仅类型检查):
from dataclasses import asdict, dataclass
from typing import List, Union
from validated_dc import ValidatedDC
@dataclass
class Foo(ValidatedDC):
foo: str
@dataclass
class Bin(ValidatedDC):
bin: str
@dataclass
class Items(ValidatedDC):
some_list: List[Union[Foo, Bin]]
data = [
{
"foo": "bar"
},
{
"bin": "baz"
}
]
items = Items(some_list=data)
assert items.get_errors() is None
assert isinstance(items.some_list[0], Foo)
assert isinstance(items.some_list[1], Bin)
assert asdict(items) == {'some_list': data}
ValidatedDC-https://github.com/EvgeniyBurdin/validated_dc