0

I am not able to extract the "Data" "12639735;7490484;3469776;9164745;650;0" from this file using python: In php it's piece of cake for me but I cannot master it in python. Other answers from Stackexchange didn't give me the answer. Here is the contents of the file 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"
}
miken32
  • 42,008
  • 16
  • 111
  • 154
HvdW
  • 1
  • 1
  • 4
  • Possible duplicate of [Parsing values from a JSON file using Python?](http://stackoverflow.com/questions/2835559/parsing-values-from-a-json-file-using-python) – Jack Evans May 11 '17 at 22:30

2 Answers2

2

This should work

import json

with open('test.json') as f:
    contents = json.load(f)
    print(contents['result'][0]['Data'])

Similar questions have been asked before: Parsing values from a JSON file using Python?

Community
  • 1
  • 1
Jack Evans
  • 1,697
  • 3
  • 17
  • 33
0

Got it.

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"])
HvdW
  • 1
  • 1
  • 4