【问题标题】:Finding values in JSON type response (HTTP) in python在 python 中查找 JSON 类型响应 (HTTP) 中的值
【发布时间】:2021-01-12 16:20:11
【问题描述】:

我有一个 json 响应

{"length": 2, "chain": [{"index": 0, "transactions": [], "timestamp": 0, "previous_hash": "0", "nonce": 0, "hash": "6dbf23122cb5046cc5c0c1b245c75f8e43c59ca8ffeac292715e5078e631d0c9"}, {"index"
: 1, "transactions": [{"test": "test2.m4a", "ipfs_hash": "QmaQgbSbgZdAfdiHmnfD9PevwpYSepytvA5G6agupqjVA3", "timestamp": 1610467381.102983, "Mac_ADD": "e0:94:67:bc:17:f9", "signature": "b'AQAgIQgAAD'"}], "timestamp":1610467386.9993732,"previous_hash":"6dbf23122cb5046cc5c0c1b245c75f8e43c59ca8ffeac292715e5078e631d0c9", "nonce": 214, "hash": "00a92715e60f577a57165b52c02e2b6bbef1ba60a3a27ead16e4d6a7a0a9d3d2"}], "peers": []}

我想使用“test”值找到对应的“ipfs_hash”。如何在 python 中实现这一点?

【问题讨论】:

  • 您好,您应该在此处发布您已经开发的代码,以便人们可以提供帮助 - Python 的起点是使用 json.loads 解析 JSON 字符串。

标签: python json list dictionary


【解决方案1】:

正如其他人所提到的,您可以使用json 将您的字符串加载到 python 对象中,例如字典。

s = '''{"length": 2, "chain": [{"index": 0, "transactions": [], "timestamp": 0, "previous_hash": "0", "nonce": 0, "hash": "6dbf23122cb5046cc5c0c1b245c75f8e43c59ca8ffeac292715e5078e631d0c9"}, {"index"
: 1, "transactions": [{"test": "test2.m4a", "ipfs_hash": "QmaQgbSbgZdAfdiHmnfD9PevwpYSepytvA5G6agupqjVA3", "timestamp": 1610467381.102983, "Mac_ADD": "e0:94:67:bc:17:f9", "signature": "b'AQAgIQgAAD'"}], "timestamp":1610467386.9993732,"previous_hash":"6dbf23122cb5046cc5c0c1b245c75f8e43c59ca8ffeac292715e5078e631d0c9", "nonce": 214, "hash": "00a92715e60f577a57165b52c02e2b6bbef1ba60a3a27ead16e4d6a7a0a9d3d2"}], "peers": []}'''

import json

d = json.loads(s)

test = 'test2.m4a'
for c in d['chain']:
    for t in c['transactions']:
        if t['test'] == test:
            print(t['ipfs_hash'])

【讨论】:

    【解决方案2】:

    您可以使用 Python 标准库模块 json 通过 json.loads(json_string) 函数将 JSON 字符串转换为字典。

    例如:

    import json
    my_dictionary = json.loads("""{"length": 2, "chain": [{"index": 0, "transactions": [], "timestamp": 0, "previous_hash": "0", "nonce": 0, "hash": "6dbf23122cb5046cc5c0c1b245c75f8e43c59ca8ffeac292715e5078e631d0c9"}, {"index" : 1, "transactions": [{"test": "test2.m4a", "ipfs_hash": "QmaQgbSbgZdAfdiHmnfD9PevwpYSepytvA5G6agupqjVA3", "timestamp": 1610467381.102983, "Mac_ADD": "e0:94:67:bc:17:f9", "signature": "b'AQAgIQgAAD'"}], "timestamp":1610467386.9993732,"previous_hash":"6dbf23122cb5046cc5c0c1b245c75f8e43c59ca8ffeac292715e5078e631d0c9", "nonce": 214, "hash": "00a92715e60f577a57165b52c02e2b6bbef1ba60a3a27ead16e4d6a7a0a9d3d2"}], "peers": []}""")
    

    一旦你把它当作字典,你就可以通过像这样的键索引来访问任何元素:

    print(my_dictionary["length"])
    print(my_dictionary["transactions"])
    

    【讨论】:

    • 我尝试过类似的 'url = '127.0.0.1:8000/chain' r = requests.get(url) data = r.json() print(data['transactions']) ' 但我得到了一个错误提示 KeyError: 'transactions'
    • 如果你得到一个 KeyError,那么很可能变量 r 中的数据不是你想象的那样。也许您在请求网页时收到错误消息?
    • 网页没有错误,我打印出来的数据是正确的,我检查了data的类型,它返回了dict
    • 我认为 requests.get() 不会直接返回 json 响应;它返回一个包含其他信息的字典,比如状态码。您必须在 data 中找到您的回复所在的键,然后在 that 中查找 transactions
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-05
    • 1970-01-01
    • 2018-10-17
    • 2014-01-25
    • 2020-05-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多