【发布时间】:2020-12-15 07:51:13
【问题描述】:
我有一个与多个 API 交互的 python 代码。所有的 API 都返回一些 json,但每个都有不同的结构。假设我正在所有这些 json 中查找人名:
json_a = {
"people": [
{"name": "John"},
{"name": "Peter"}
]
}
json_b = {
"humans": {
"names": ["Adam", "Martin"]
}
}
正如您在上面看到的,来自 json 的字典具有任意结构。我想定义一些东西,作为导航每个 json 的“蓝图”,如下所示:
all_jsons = {
"json_a": {
"url": "http://endpoint",
"json_structure": "people -> list -> name"
},
"json_b": {
"url": "http://someotherendpoint",
"json_structure": "humans -> names -> list"
}
}
因此,如果我正在使用json_a,我只需查看all_jsons["json_a"]["json_structure"],我就会了解如何导航这个确切的 json。实现这一目标的最佳方法是什么?
【问题讨论】:
标签: python json dictionary