You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python解析嵌套JSON获取etoday值失败的技术求助

正确获取嵌套JSON中etoday值的方法

正确取值代码

你需要通过以下路径访问etoday,核心是注意records是数组类型,必须通过索引(比如[0])访问其中的元素:

Result = json.loads(json_string)
# 正确获取etoday的写法
etoday_value = Result['data']['page']['records'][0]['etoday']
print("The value of etoday is:", etoday_value)

你的错误写法分析

逐个看你尝试的写法问题:

  • print(Result['data']['page']['records']['etoday']):records是数组(JSON中用[]包裹),不能直接通过键名etoday访问,必须先通过索引定位数组里的对象。
  • print(Result["data"][0]["page"][0]["etoday"]):data和page都是JSON对象(用{}包裹),不是数组,不能用[0]索引访问。
  • print(Result['page']['records'][0]['etoday']):缺少了data层级,page是嵌套在data对象里的,直接访问Result['page']会找不到对应键。
  • Print("The value of", "etoday", "is: ", Result["etoday"]):etoday不在JSON根节点,而且Python中打印函数是小写的print,大写会报错。

完整可运行示例

import json

json_string = '''
{  
  "success": true,  
  "code": "0",  
  "msg": "success",  
  "data": {    
    "inverterStatusVo": {      
      "all": 1,      
      "normal": 1,      
      "fault": 0,      
      "offline": 0,      
      "mppt": 0    
    },    
    "page": {      
      "records": [        
        {          
          "id": "zzz",          
          "sn": "xxx",
          "etoday": 10.900
          // 省略其他冗余字段
        }      
      ],      
      "total": 1,      
      "size": 20,      
      "current": 1,      
      "orders": [],      
      "optimizeCountSql": false,      
      "searchCount": true,      
      "pages": 1    
    },    
    "mpptSwitch": 0  
  }
}
'''

Result = json.loads(json_string)
print("The value of msg is:", Result["msg"])
# 正确获取etoday
print("The value of etoday is:", Result['data']['page']['records'][0]['etoday'])

内容的提问来源于stack exchange,提问作者user1438082

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 15:41:47