Python在JSON结构中获取 sibling

标签 python json dictionary

假设我得到了目标并想要目标。您将如何迭代并在一个类轮中获得所需的目标值(给定例如 A1)。我使用 json.load(json_data)

从文件中获取数据没有问题

JSON

{
  "conservative": {
    "goal": "A1",
    "target" : 60000,
    "oneoff": 6000,
    "regular": 600
  },
  "moderate": {
    "goal": "A2",
    "target": 70000,
    "oneoff": 7000,
    "regular": 710
  },
  "optimistic": {
    "goal": "A3",
    "target": 80000,
    "oneoff": 8000,
    "regular": 800
  }
}

最佳答案

也许您可以尝试使用以下方法获取可能与输入值匹配的目标列表(如果需要单个值,您可以简单地将 [0] 放入从列表中获取单个项目)。例如。使用 goal_val = 'A1':

target = [val['target'] for _, val in my_data.items() if val['goal'] == goal_val]

关于Python在JSON结构中获取 sibling ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54618964/

相关文章:

python - 根据字典值和 python 中的一些条件创建列表

python - 如何对dict元素求和

jquery - 使用 jQuery 从 JSON 返回嵌套值

python - 如何从 plt.imshow() 获取数据?

python - 通过 Chef Recipe 激活 virtualenv 并安装 python 库

python - Django:属性错误: "Object has no attribute"

android - DialogBox 导致应用程序崩溃。

php - 通过 AJAX 发布在 PHP 中回显 JSON 对象

dictionary - F# 基础 : Map of Maps?

python - 从列表中删除重复项(算法速度)