如何从json中提取数据到字符串中

时间:2017-05-11 22:24:51

标签: python json extract

我无法提取"数据" " 12639735; 7490484; 3469776; 9164745; 650; 0" 从这个文件使用python: 在PHP中它对我来说是一块蛋糕,但我无法在python中掌握它。 Stackexchange的其他答案并没有给我答案。 这是test.json文件的内容:

{
   "ActTime" : 1494535483,
   "ServerTime" : "2017-05-11 22:44:43",
   "Sunrise" : "05:44",
   "Sunset" : "21:14",
   "result" : [
      {
         "AddjMulti" : 1.0,
         "AddjMulti2" : 1.0,
         "AddjValue" : 0.0,
         "AddjValue2" : 0.0,
         "BatteryLevel" : 255,
         "Counter" : "20130.221",
         "CounterDeliv" : "12634.521",
         "CounterDelivToday" : "0.607 kWh",
         "CounterToday" : "1.623 kWh",
         "CustomImage" : 0,
         "Data" : "12639735;7490484;3469776;9164745;650;0",
         "Description" : "",
         "Favorite" : 1,
         "HardwareID" : 3,
         "HardwareName" : "Slimme Meter",
         "HardwareType" : "P1 Smart Meter USB",
         "HardwareTypeVal" : 4,
         "HaveTimeout" : false,
         "ID" : "1",
         "LastUpdate" : "2017-05-11 22:44:39",
         "Name" : "Elektriciteitsmeter",
         "Notifications" : "false",
         "PlanID" : "0",
         "PlanIDs" : [ 0 ],
         "Protected" : false,
         "ShowNotifications" : true,
         "SignalLevel" : "-",
         "SubType" : "Energy",
         "SwitchTypeVal" : 0,
         "Timers" : "false",
         "Type" : "P1 Smart Meter",
         "TypeImg" : "counter",
         "Unit" : 1,
         "Usage" : "650 Watt",
         "UsageDeliv" : "0 Watt",
         "Used" : 1,
         "XOffset" : "0",
         "YOffset" : "0",
         "idx" : "1"
      }
   ],
   "status" : "OK",
   "title" : "Devices"
}

1 个答案:

答案 0 :(得分:0)

知道了。

url = "http://192.168.2.1:8080/json.htm?type=devices&rid=1"
response = urllib.urlopen(url)
str = json.loads(response.read())
for i in str["result"]:
  datastring = i["Data"]
  elementstring =  i["Data"].split(';')
  counter = 0
  for j in elementstring:
    if counter == 4:
      usage = j
    counter += 1
  delivery = get_num(i["UsageDeliv"])