2017-09-08 34 views
-2

我通過使用Idents[0].IsVerified來做到這一點,但我不想提供索引號。我需要檢查哪個塊與PrimaryEmailMobile有關。這是我的JSON:如何在不傳入索引號的情況下獲取值JSON數組?

"Idents": [ 
    { 
    "PrimaryEmail": "[email protected]", 
    "IsVerified": false, 
    "IdentId": 1, 
    "EmailVerificationCode": 302284 
    }, 
    { 
    "Mobile": "1234567890", 
    "IsVerified": true, 
    "IdentId": 2, 
    "MobileVerificationCode": 302284 
    }, 
    { 
    "CardNumber": 0, 
    "IsVerified": false, 
    "IdentId": 4 
    } 
] 
+0

您期望得到什麼_exactly_?舉個例子。 – DyZ

+0

我有PrimaryEmail,我想檢查哪個塊包含它。 –

+0

使用循環或列表理解。 – Barmar

回答

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'}] 
相關問題