python读取json复合属性[已关闭]

arknldoa  于 2022-12-02  发布在  Python
关注(0)|答案(1)|浏览(106)

编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答问题。
44分钟前关门了。
Improve this question
我的Json文件有以下内容

{
  "role": [
    {
      "type": "account",
      "attributes": {
        "order": 50
      }
    },
 {
      "type": "secretary",
      "attributes": {
        "order": 10
      }
    },
 {
      "type": "account",
      "attributes": {
        "order": 3
      }
    }
]
}

我想得到所有角色之间的最大顺序的角色。我怎么能在python中做到这一点呢?
我已尝试以下方法:

lowestpriority = roles.attributes[max('order')]

但它给出了一个错误:属性错误:'list'对象没有属性'attributes'

kqlmhetl

kqlmhetl1#

你是JS出身?
事实上列表没有这样的属性。假设角色是你的列表(因为你没有提供任何代码):

max_order_role = sorted(roles, key= lambda i:i.get("attributes",{}).get("order"))[-1]

相关问题