【问题标题】:Python extracting JSON dictionary TypeErrorPython 提取 JSON 字典 TypeError
【发布时间】:2020-05-11 15:46:22
【问题描述】:

我正在使用this Python project 尝试与我的乐高 SBrick 互动。

如果我使用以下代码(一旦连接到 SBrick):

json_response = client.rr_get_adc(sbrick_id='11:22:33:44:55:66', timeout=5)

然后我可以打印 json_response 给我:

{"ret_code": 100, "temperature": 42.53253535, "voltage": 8.3954548125663}

我想访问“ret_code”来检查它是否等于 100,但如果我尝试:

print (json_response["ret_code"])

我收到此错误:

TypeError: string indices must be integers

如何将 ret_code 提取为整数?

【问题讨论】:

标签: python json string dictionary


【解决方案1】:

感谢 jonrsharpe。

我补充说:

result = json.loads(json_response)

然后我可以通过以下方式访问 ret_code:

result["ret_code"]

【讨论】:

    猜你喜欢
    • 2015-12-04
    • 2017-12-05
    • 1970-01-01
    • 2022-08-02
    • 1970-01-01
    • 1970-01-01
    • 2019-03-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多