有任何想法如何在python中解析这个有效负载?

时间:2016-07-28 04:00:48

标签: python json parsing data-structures iot

我正在使用MQTT订阅主题并接收消息。到目前为止我已经编写了这个代码并且它可以正常订阅,但我确定了解析从MQTT代理接收的有效负载的方法时遇到了问题。我一直试图获得我在有效载荷中看到的数值,但我无法找到解析这些数据的正确方法。有什么想法/建议吗?谢谢!

我的代码:

有更多不相关的代码。这只是一个片段。

import paho.mqtt.client as mqtt
import json

#Call back functions 

# gives connection message
def on_connect(client,userdata,rc):
print("Connected with result code:"+str(rc))
# subscribe for all devices of user
client.subscribe('+/devices/+/up')

def on_connect(client,userdata,rc):
print("Connected with result code:"+str(rc))
# subscribe for all devices of user
client.subscribe('+/devices/+/up')

def on_message(client,userdata,msg):
print"Topic",msg.topic + "\nMessage:" + str(msg.payload)

node_data = str(msg.payload)
print '\r\n test'
print node_data

j = json.loads(node_data)
print "\r\n after loads: "
print j

我的输出

Message:{"payload":"Dgv+CggGBCYFAPMLAgc=","fields":{"Light":58.32,"Pressure":98569.5,"Temp":32.4375,"X_accel":0.6875,"Y_accel":-0.125,"Z_accel":0.625},"port":1,"counter":8,"dev_eui":"000000007D9050C1","metadata":[{"frequency":904.3,"datarate":"SF7BW125","codingrate":"4/5","gateway_timestamp":2090979635,"gateway_time":"2016-07-28T02:26:15.386371Z","channel":2,"server_time":"2016-07-28T02:06:13.075194806Z","rssi":-13,"lsnr":9.5,"rfchain":0,"crc":1,"modulation":"LORA","gateway_eui":"0080000000009BE6","altitude":911,"longitude":-93.19677,"latitude":45.10303}]}


 test
{"payload":"Dgv+CggGBCYFAPMLAgc=","fields":{"Light":58.32,"Pressure":98569.5,"Temp":32.4375,"X_accel":0.6875,"Y_accel":-0.125,"Z_accel":0.625},"port":1,"counter":8,"dev_eui":"000000007D9050C1","metadata":[{"frequency":904.3,"datarate":"SF7BW125","codingrate":"4/5","gateway_timestamp":2090979635,"gateway_time":"2016-07-28T02:26:15.386371Z","channel":2,"server_time":"2016-07-28T02:06:13.075194806Z","rssi":-13,"lsnr":9.5,"rfchain":0,"crc":1,"modulation":"LORA","gateway_eui":"0080000000009BE6","altitude":911,"longitude":-93.19677,"latitude":45.10303}]}


 after loads: 
{u'fields': {u'Y_accel': -0.125, u'Temp': 32.4375, u'X_accel': 0.6875, u'Light': 58.32, u'Pressure': 98569.5, u'Z_accel': 0.625}, u'counter': 8, u'port': 1, u'dev_eui': u'000000007D9050C1', u'payload': u'Dgv+CggGBCYFAPMLAgc=', u'metadata': [{u'gateway_time': u'2016-07-28T02:26:15.386371Z', u'server_time': u'2016-07-28T02:06:13.075194806Z', u'datarate': u'SF7BW125', u'gateway_eui': u'0080000000009BE6', u'modulation': u'LORA', u'gateway_timestamp': 2090979635, u'longitude': -93.19677, u'crc': 1, u'frequency': 904.3, u'rfchain': 0, u'codingrate': u'4/5', u'lsnr': 9.5, u'latitude': 45.10303, u'rssi': -13, u'altitude': 911, u'channel': 2}]}

我希望能够从这个...字符串中提取数值?Dict?Json?(有效载荷)用于' Temp'' Light',&#39 ;压力'等...参数。非常感谢任何建议。' 我尝试了以下但我收到此错误。不会告诉我太多......

data = json.loads(node_data)
Press = data['Pressure']
print Press

文件" C:\ Python27 \ My_Py_27_Codes \ scratch_py_mqtt.py",第25行,在on_message中 按=数据['压力']

KeyError: 'Pressure'

显然'压力'这本词典不是关键吗?

2 个答案:

答案 0 :(得分:2)

node_data  = {"payload":"dsgsg","fields":{"pressure":34,"temp":35}}
data = json.loads(node_data)
Press = data['fields']['Pressure']
print Press

您必须提供提交的确切位置。在您的情况下,pressure位于fields字典

答案 1 :(得分:0)

所以经过几个小时的挠头和编码后#39;走了,我设法实现了我的目标。我不确定它是否是最漂亮的'方式或者它是否是最好的方式,但这是我想到的:我正在处理一个'词典'我必须选择适当的键名字段'。这将使我能够获得'与'字段内的参数(温度,压力,光等)相关的数值。字典的一部分。

解决我的问题的代码:

def on_message(client,userdata,msg):
print"Topic",msg.topic + "\n\nMessage:" + str(msg.payload)

node_data = str(msg.payload)

print ' \n Sensor Values \r'

my_dict = json.loads(node_data)
params = my_dict.get("fields",None)

light = params.get('Light')
pressure = params.get('Pressure')
temp = params.get('Temp')
x = params.get('X_accel')
y = params.get('Y_accel')
z = params.get('Z_accel')

print ''
print 'Light:' + str(light)
print 'Pressure:' + str(pressure)
print 'Temperature:' + str(temp)
print 'X-accel:' + str(x)
print 'Y-accel:' + str(y)
print 'Z-accel:' + str(z)
print '\n\n'

为每个人欢呼!