查询嵌套JSON数据

iqxoj9l9  于 2023-02-06  发布在  其他
关注(0)|答案(2)|浏览(139)

我尝试使用下面的代码进行查询,以获取值etoday,但它没有返回结果。

Result = json.loads(json_string)
  # Next Line works perfect
  print("The value of msg", "msg", "is: ", Result["msg"])
  #Next Line does not (I have a few variations here that I tried
  print(Result['data']['page']['records']['etoday'])
  print(Result["data"][0]["page"][0]["etoday"])
  print(Result['page']['records'][0]['etoday'])
  Print("The value of", "etoday", "is: ", Result["etoday"])

我正在查询的文件

{
  "success": true,
  "code": "0",
  "msg": "success",
  "data": {
    "inverterStatusVo": {
      "all": 1,
      "normal": 1,
      "fault": 0,
      "offline": 0,
      "mppt": 0
    },
    "page": {
      "records": [
        {
          "id": "zzz",
          "sn": "xxx",
          "collectorSn": "vvv",
          "userId": "ttt",
          "productModel": "3105",
          "nationalStandards": "68",
          "inverterSoftwareVersion": "3d0037",
          "dcInputType": 2,
          "acOutputType": 0,
          "stationType": 0,
          "stationId": "1298491919448828419",
          "rs485ComAddr": "101",
          "simFlowState": -1,
          "power": 6.000,
          "powerStr": "kW",
          "pac": 0.001,
          "pac1": 0,
          "pacStr": "kW",
          "state": 1,
          "stateExceptionFlag": 0,
          "fullHour": 1.82,
          "totalFullHour": 191.83,
          "maxDcBusTime": "1675460767377",
          "maxUac": 251.5,
          "maxUacTime": "1664022228160",
          "maxUpv": 366.3,
          "maxUpvTime": "1663309960961",
          "timeZone": 0.00,
          "timeZoneStr": "(UTC+00:00)",
          "timeZoneName": "(UTC+00:00)Europe/Dublin",
          "dataTimestamp": "1675460767377",
          "dataTimestampStr": "2023-02-03 21:46:07 (UTC+00:00)",
          "fisTime": "1663189116371",
          "inverterMeterModel": 5,
          "updateShelfBeginTime": 1671292800000,
          "updateShelfEndTime": 1829059200000,
          "updateShelfEndTimeStr": "2027-12-18",
          "updateShelfTime": "5",
          "collectorId": "1306858901387600551",
          "dispersionRate": 0.0,
          "currentState": "3",
          "pow1": 0.0,
          "pow2": 0.0,
          "pow3": 0.0,
          "pow4": 0.0,
          "pow5": 0.0,
          "pow6": 0.0,
          "pow7": 0.0,
          "pow8": 0.0,
          "pow9": 0.0,
          "pow10": 0.0,
          "pow11": 0.0,
          "pow12": 0.0,
          "pow13": 0.0,
          "pow14": 0.0,
          "pow15": 0.0,
          "pow16": 0.0,
          "pow17": 0.0,
          "pow18": 0.0,
          "pow19": 0.0,
          "pow20": 0.0,
          "pow21": 0.0,
          "pow22": 0.0,
          "pow23": 0.0,
          "pow24": 0.0,
          "pow25": 0.0,
          "pow26": 0.0,
          "pow27": 0.0,
          "pow28": 0.0,
          "pow29": 0.0,
          "pow30": 0.0,
          "pow31": 0.0,
          "pow32": 0.0,
          "gridPurchasedTodayEnergy": 3.800,
          "gridPurchasedTodayEnergyStr": "kWh",
          "gridSellTodayEnergy": 2.400,
          "gridSellTodayEnergyStr": "kWh",
          "psumCalPec": "1",
          "batteryPower": 0.099,
          "batteryPowerStr": "kW",
          "batteryPowerPec": "1",
          "batteryCapacitySoc": 20.000,
          "parallelStatus": 0,
          "parallelAddr": 0,
          "parallelPhase": 0,
          "parallelBattery": 0,
          "batteryTodayChargeEnergy": 4.800,
          "batteryTodayChargeEnergyStr": "kWh",
          "batteryTotalChargeEnergy": 449.000,
          "batteryTotalChargeEnergyStr": "kWh",
          "batteryTodayDischargeEnergy": 5.500,
          "batteryTodayDischargeEnergyStr": "kWh",
          "batteryTotalDischargeEnergy": 627.000,
          "batteryTotalDischargeEnergyStr": "kWh",
          "bypassLoadPower": 0.000,
          "bypassLoadPowerStr": "kW",
          "backupTodayEnergy": 0.000,
          "backupTodayEnergyStr": "kWh",
          "backupTotalEnergy": 0.000,
          "backupTotalEnergyStr": "kWh",
          "etotal": 1.153,
          "etoday": 10.900,
          "psum": -1.756,
          "psumCal": -1.756,
          "etotal1": 1153.000,
          "etoday1": 10.900000,
          "offlineLongStr": "--",
          "etotalStr": "MWh",
          "etodayStr": "kWh",
          "psumStr": "kW",
          "psumCalStr": "kW"
        }
      ],
      "total": 1,
      "size": 20,
      "current": 1,
      "orders": [
        
      ],
      "optimizeCountSql": false,
      "searchCount": true,
      "pages": 1
    },
    "mpptSwitch": 0
  }
}
dly7yett

dly7yett1#

records键是路径中唯一的数组,也是唯一需要使用如下索引进行选择的数组:

Result['data']['page']['records'][0]['etoday']
zmeyuzjn

zmeyuzjn2#

问题可能是密钥etoday不存在于您尝试访问它的位置。请尝试使用以下代码查看JSON对象中是否存在该密钥:
如果结果[“数据”][“页面”][“记录”]中为“今天”:print(“今天的值是:“,结果['数据']['页面']['记录']['今天'])否则:print(“在结果['data']['page']['records']字典中找不到关键字'etoday'。”)

相关问题