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"
}
1

2 Answers 2

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?

Sign up to request clarification or add additional context in comments.

Comments

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"])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.