python JSON返回出错?-嵌套的废话

balp4ylt  于 2022-11-28  发布在  Python
关注(0)|答案(1)|浏览(75)

我有一个通过curl从JSON调用返回的结果

{
    'result': {
        'today_runtime': 830,
        'month_runtime': 39991,
        'today_energy': 1293,
        'month_energy': 55326,
        'local_time': '2022-11-27 13:50:54',
        'electricity_charge': \[0, 0, 0\],
        'current_power': 93860
    },
    'error_code': 0
}

现在,我大约花了5分钟的时间进入python,实际上更像是45分钟,因为这是超级令人沮丧的,在任何其他语言中,我现在都已经排序了,但是我需要内部括号部分中的键值对中的值,我知道如何在没有

{'result': 

and 

, 'error_code': 0}

as i have manually chopped them off and get the expected result when extracting values

someone please put mne out of what is suddently becoming a misery, thanks in advance....

花了45分钟在谷歌上搜索如何修剪子字符串,访问嵌套值等....似乎都不起作用...。

k10s72fa

k10s72fa1#

{
    'result': {
        'today_runtime': 830,
        'month_runtime': 39991,
        'today_energy': 1293,
        'month_energy': 55326,
        'local_time': '2022-11-27 13:50:54',
        'electricity_charge': \[0, 0, 0\],
        'current_power': 93860
    },
    'error_code': 0
}

那是一本字典。它有两个关键字:“结果”和“错误代码”。
“result”键的值也是一个字典,有七个键。
(The“electricity_charge”键很奇怪。为什么反斜杠\[\]会出现在那里?我猜这是您的格式问题。)
假设您将此字典指派给名为data的变数,您会以下列方式存取信息:

data['result']['today_runtime']
data['result']['month_runtime']
...
data['result']['current_power']
data['error_code']

这些都是非常直接的字典语法,一点也不“废话”。

相关问题