Hej!
Hur sjutton kommer man åt ett object i en collection via json? Jag har testat diverse syntax/tutorials, men måste missa nåt. Jag vill plocka ut "value" från en Aeon 4-in-1
{"id":38,"name":"Framsida - Temp","roomID":3,"type":"temperature_sensor","properties":{"UIMessageSendTime":"0","classConfigure":"","classGeneric":"49","classSupport":"","classVersion":"","dead":"0","deviceControlType":"0","deviceIcon":"30","disabled":"0","emailNotificationID":"0","emailNotificationType":"0","endPoint":"1","isBatteryOperated":"0","log":"","logTemp":"","needConfigure":"5","nodeID":"17","parametersTemplate":"0","parentID":"37","pollingRetryError":"0","pollingTime":"","pollingTimeNext":"","pollingTimeSec":"0","productInfo":"0,134,0,2,0,5,1,18","pushNotificationID":"0","pushNotificationType":"0","showChildren":"0","showFireAlarm":"0","showFreezeAlarm":"0","smsNotificationID":"0","smsNotificationType":"0","sortOrder":"999","unit":"C","useTemplate":"0","userDescription":"","value":"-0.9","zwaveCompany":"Aeon Labs","zwaveInfo":"","zwaveVersion":"1,18","parameters":[],"associationView":[],"associationSet":[]},"actions":{},"created":1385584470,"modified":1385584470,"sortOrder":1}
#!/usr/bin/python
import json
import urllib2
from pprint import pprint
#Lets define the root of our system
SERVER = '172.25.74.5'
authinfo = urllib2.HTTPPasswordMgrWithDefaultRealm()
authinfo.add_password(None, SERVER, 'myuser', 'mypass')
page = 'HTTP://'+SERVER+'//api/devices?id=38'
handler = urllib2.HTTPBasicAuthHandler(authinfo)
myopener = urllib2.build_opener(handler)
opened = urllib2.install_opener(myopener)
output = urllib2.urlopen(page)
the_page = output.read()
j_obj = json.loads(the_page)
pprint(j_obj)
print j_obj['properties:value']
Så, jodå, jag kan dumpa nycklarna, men inte nå specicika undernycklar
pi@krokopool /opt/Krokopool/bin $ ./slask.py
{u'actions': {},
u'created': 1385584470,
u'id': 38,
u'modified': 1385584470,
u'name': u'Framsida - Temp',
u'properties': {u'UIMessageSendTime': u'0',
u'associationSet': [],
u'associationView': [],
u'classConfigure': u'',
u'classGeneric': u'49',
u'classSupport': u'',
u'classVersion': u'',
u'dead': u'0',
u'deviceControlType': u'0',
u'deviceIcon': u'30',
u'disabled': u'0',
u'emailNotificationID': u'0',
u'emailNotificationType': u'0',
u'endPoint': u'1',
u'isBatteryOperated': u'0',
u'log': u'',
u'logTemp': u'',
u'needConfigure': u'5',
u'nodeID': u'17',
u'parameters': [],
u'parametersTemplate': u'0',
u'parentID': u'37',
u'pollingRetryError': u'0',
u'pollingTime': u'',
u'pollingTimeNext': u'',
u'pollingTimeSec': u'0',
u'productInfo': u'0,134,0,2,0,5,1,18',
u'pushNotificationID': u'0',
u'pushNotificationType': u'0',
u'showChildren': u'0',
u'showFireAlarm': u'0',
u'showFreezeAlarm': u'0',
u'smsNotificationID': u'0',
u'smsNotificationType': u'0',
u'sortOrder': u'999',
u'unit': u'C',
u'useTemplate': u'0',
u'userDescription': u'',
u'value': u'-1.2',
u'zwaveCompany': u'Aeon Labs',
u'zwaveInfo': u'',
u'zwaveVersion': u'1,18'},
u'roomID': 3,
u'sortOrder': 1,
u'type': u'temperature_sensor'}
{u'classVersion': u'', u'emailNotificationType': u'0', u'endPoint': u'1', u'associationView': [], u'deviceIcon': u'30', u'classSupport': u'', u'classConfigure': u'', u'deviceControlType': u'0', u'nodeID': u'17', u'dead': u'0', u'disabled': u'0', u'productInfo': u'0,134,0,2,0,5,1,18', u'parentID': u'37', u'isBatteryOperated': u'0', u'emailNotificationID': u'0', u'unit': u'C', u'pushNotificationID': u'0', u'log': u'', u'parameters': [], u'pollingTimeSec': u'0', u'zwaveInfo': u'', u'pollingTimeNext': u'', u'sortOrder': u'999', u'zwaveCompany': u'Aeon Labs', u'classGeneric': u'49', u'zwaveVersion': u'1,18', u'smsNotificationID': u'0', u'useTemplate': u'0', u'smsNotificationType': u'0', u'logTemp': u'', u'pushNotificationType': u'0', u'showFreezeAlarm': u'0', u'parametersTemplate': u'0', u'associationSet': [], u'showFireAlarm': u'0', u'needConfigure': u'5', u'showChildren': u'0', u'pollingRetryError': u'0', u'pollingTime': u'', u'value': u'-1.2', u'userDescription': u'', u'UIMessageSendTime': u'0'}
pi@krokopool /opt/Krokopool/bin $