【问题标题】:How to get value JSON array without passing index number in Python?如何在不通过 Python 中的索引号的情况下获取值 JSON 数组?
【发布时间】:2017-09-08 04:43:47
【问题描述】:

我通过使用Idents[0].IsVerified 来做到这一点,但我不想提供索引号。我需要检查哪个块与PrimaryEmailMobile 相关联。这是我的 JSON:

"Idents": [
  {
    "PrimaryEmail": "abc@gmail.com",
    "IsVerified": false,
    "IdentId": 1,
    "EmailVerificationCode": 302284
  },
  {
    "Mobile": "1234567890",
    "IsVerified": true,
    "IdentId": 2,
    "MobileVerificationCode": 302284
  },
  {
    "CardNumber": 0,
    "IsVerified": false,
    "IdentId": 4
  }
]

【问题讨论】:

  • 您希望得到什么确切?举个例子。
  • 我有 PrimaryEmail,我想检查哪个块包含它。
  • 使用循环或列表推导。

标签: python arrays json


【解决方案1】:

Python 确实直接支持 JSON。首先,您必须将 JSON 转换为列表和字典(模块 json 具有必要的工具)。假设您已完成转换并且 JSON 数组在列表 Idents 中,请查看列表并检查哪些列表项具有键 "Mobile"

[block for block in Idents if "Mobile" in block]
#[{'MobileVerificationCode': 302284, 'IdentId': 2, 'IsVerified': True, 
#  'Mobile': '1234567890'}]

过滤也可以得到同样的结果:

list(filter(lambda block: "Mobile" in block, Idents))
#[{'MobileVerificationCode': 302284, 'IdentId': 2, 'IsVerified': True, 
#  'Mobile': '1234567890'}]

【讨论】:

    猜你喜欢
    • 2022-08-20
    • 2019-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-27
    相关资源
    最近更新 更多