【发布时间】:2018-09-12 15:35:50
【问题描述】:
Json 数据:
{
"options": {
"name": "aaa",
"count": 20,
"a1": 30
},
"PC": {
"processor": "Intel",
"os": "windows"
}
}
图案:
{
"options": {
"name": "string",
"count": "integer",
"a1": "integer"
},
"PC": {
"processor": "string",
"os": "string"
}
}
如何检查所有的键都在那里并且它们的类型是否相同?
import json
data = '{"options": {"name": "aaa","count": 20,"a1": 30},"PC": {"processor": "Intel","os": "windows"}}'
pattern = '{"options": {"name": "string","count": "integer","a1": "integer"},"PC": {"processor": "string","os": "string"}}'
json_data = json.loads(data)
json_pattern = json.loads(pattern)
for key in json_pattern.keys():
if key not in json_data:
print("Error: %s" % key)
print("end")
json_pattern.keys() 只返回options 和PC,但我也需要name, count, a1, processor, os。
还有一个问题,如何知道PC 和options 是包含另一个变量的变量?
对不起我的英语。
【问题讨论】:
-
你试过写代码吗?
-
非常相关,只是使用Python类型而不是类型名称:stackoverflow.com/q/45812387/1639625
标签: python json python-jsonschema