簡體   English   中英

如何從鍵值中獲取並從該值中獲取另一個值

[英]How to take from key value and take from this value another value

我如何獲得所需的價值,因為我將發布請求發送到其他站點並且無法編輯站點的答案。

我從響應內容中得到了這個 dict:

{'username': 'DeadFinder', 'subscriptions': [{'subscription': 'default', 'expiry': '1635683460'}], 'ip': 'not at this life'}

您如何在此字典中看到有一個關鍵subscriptions ,我需要值expiry (這是時間戳)但是如果當我嘗試調用此值時我沒有看到任何結果(代碼),我如何獲得此值沒有給出所需的價值),也許有任何變體如何獲得這個價值? 我沒有找到這樣的東西。

也許我的一小部分代碼可以幫助你,但我懷疑。

    data1 = {f"hwid":"", "type":"login", "username": {username}, "pass": {password}, 
    "sessionid":f"{response_cut2}", "name":"test_app", "ownerid":"5OLbm5S3fS"}
    url1 = "nope"
    response1 = requests.post(url1, data1)
    data = response1.json()
    #get = data.get('expiry')
    file_write = open("test.txt", "w")
    file_write.write(str(data))
    file_write.close()
    for key in data.keys():
      if key == 'info':
        print (data[key])

您是否正在努力實現這一目標?

data = {'username': 'DeadFinder', 'subscriptions': [{'subscription': 'default', 'expiry': '1635683460'}], 'ip': 'not at this life'}
print(data['subscriptions'][0]['expiry'])


# first get 'subscriptions' which returns an array, 
# so use [0] to get this dict {'subscription': 'default', 'expiry': '1635683460'}
# then get 'expiry'

編輯:如果訂閱有多個值,則使用 for 循環

subscriptions = data['subscriptions']
for subscription in subscriptions:
    print(subscription['expiry'])

輸出

1635683460

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM