In the IOT IOT design and development, we usually encounter some data interaction and transmission problems, we need to transfer data between different devices for interactive communication, usually we need to choose a suitable data interaction protocol and format, so that our The design will be greatly simplified, and the design quality will be improved. JSON is a lightweight data exchange format. In recent years, it has not only been widely used in the Internet neighborhood, but also has been widely used in the IOT field. Application, using JSON for data interaction has become the optimization of many IOT design solutions. This section of the blog will be used to parse and apply JSON messages on our current popular IOT development kit draongboard 410c.
Here we use the Python script to parse the JSON message on the dragonboard 410c. Here Python provides the JSON library module to process the JSON message. Here we only need to install the JSON python module on the dragonboard 410c using the apt-get tool. Yes, the specific installation commands are as follows:
Apt-get install json-py
After installation, you can use the import json import to use the module. It is very convenient to use this module for JSON analysis. Let's see how to use JSON module to quickly parse JSON message:
Here we give a sample of commonly used system user registration messages, as follows:
'{
"report": {
"from": "MBD000000001",
"to": "MBS000000001",
“TImeStampâ€: “2017-1-14 12:45:32â€,
"msg": {
"type": 0,
"userName": "test1",
"userMD5": "abdsetsasgt",
"sexuality": "male",
"age": "19",
"phone": "15361822674",
"email": ""
}
}
}'
In this message, the sender ID, timestamp, message type, user name, user password MD5 value, user gender, etc. of the message can be sent to the dragonbaord 410c device for registration. Register a message as an example to teach you how to quickly write python scripts to parse JSON messages.
The specific python code is as follows:
#coding:utf8
Import sys
Import json
Class MssageProcessing:
Def __init__(self,recvMsg):
self.reporTIsError=0
self.reporTID=""
self.toID=""
Try:
self.reportJosn=json.loads(recvMsg)["report"]
self.msgJosn=self.reportJosn["msg"]
self.fromID=self.getFromID()
If self.fromID! = "error":
self.reporTIsError=0
Else:
self.reportIsError=1
self.toID=self.getToID()
If self.toID! = "error":
self.reportIsError=0
Else:
self.reportIsError=1
Except:
Print("recv report is error")
self.reportIsError=1
Def updateMsg(self,recvMsg):
self.reportJosn=json.loads(recvMsg)["report"]
self.msgJosn=self.reportJosn["msg"]
self.fromID=self.getFromID()
self.toID=self.getToID()
self.mobaiServiceID="MBS000000001"
Def getFromID(self):
fID=self.reportJosn["from"]
Print(fID)
If len(fID)! =12:
Return "error"
Else:
If fID[0:3]==“MBS†or fID[0:3]==“MBU†or fID[0:3]==“MBDâ€:
Try:
Int(fID[3:])
Return fID
Except:
Return "error"
Else:
Return "error"
Def getToID(self):
Print(self.reportJosn["to"])
Return self.reportJosn["to"]
Def getTimeStamp(self):
Print(self.reportJosn["timeStamp"])
Return self.reportJosn["timeStamp"]
Def getMsgType(self):
Print(self.msgJosn["type"])
Return self.msgJosn["type"]
If __name__== "__main__":
recvMsg=MssageProcessing('{"report":{"from":"000000000001","to":"000000000002","timeStamp":"2017-1-14 12:45:32","msg":{" Type":0, "userName": "test1", "userMD5": "abdsetsasgt", "sexuality": "male", "age": "19"}}}')
recvMsg.getFromID()
recvMsg.getToID()
recvMsg.getMsgType()
[userName,userMD5,sexuality,age]=recvMsg.registerMessageHandle()
Print(userName)
Print(userMD5)
After running the above code, we can see that we can get the content of the corresponding field label in the message to print, indicating that the JSON message is parsed successfully.
Middle-low Level Lighting Sky Curtain
Middle-Low Level Lighting Sky Curtain,Ultra Transparency Display,Digital Signage Video Display,High Brightness Led Outdoor Lighting
Kindwin Technology (H.K.) Limited , https://www.ktl-led.com