简体   繁体   English

JSON数据通过MQTT python接收了错误的格式

[英]JSON data receive bad format over MQTT python

I have a problem with receiving data over MQTT in Python. 我在Python中通过MQTT接收数据时遇到问题。

I send from one script: 我从一个脚本发送:

payload = '{"sensor":[{"name":"temp","value":"%s"}]}' % str(read_temp())
print payload
print is_json(payload) 
client.publish("xxxxx/xxxxx", json.dumps(payload))

and receive with another script: 并收到另一个脚本:

def on_message(client, userdata, msg):
    message = str(msg.payload)
    if is_json(message): 
        jsonResponse=json.loads(json.dumps(message))
        print jsonResponse

Output: 输出:

"{\"sensor\":[{\"name\":\"temp\",\"value\":\"23.0\"}]}"

I try play with loads and dumps but nothing helps. 我尝试玩装卸游戏,但没有任何帮助。

You double escaping the message, once at publish time and once when it's received with json.dumps . 您将消息转义两次,一次是在发布时,一次是在json.dumps收到时。 Remove the extra from the subscriber: 从订户中删除多余的内容:

def on_message(client, userdata, msg):
    message = str(msg.payload)
    if is_json(message): 
        jsonResponse=json.loads(message)
        print jsonResponse

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM