Python 到 Json 反序列化错误
Python to Json DeserializerError
我正在尝试使用 IOTHub 遥测从 PLC 读取值并使用 azure 将它们流式传输到 PowerBI。
当我 运行 代码并且能够看到集线器接收消息时,我能够连接到 PLC 并读取值。但是,流给我一个错误 'InputDeserializerError.InvalidData'。我不确定哪里出错了。请查看下面的代码并建议我如何序列化它。当我删除字符串值并且仅删除 运行 整数和浮点数时,流会拾取它。
# Define the JSON message to send to IoT Hub.
MSG_TXT = "{\"Bin1Load\": %s,\"Bin1Grower\": %s,\"Bin1Variety\": %s,\"Bin1StatedTn\": %.2f,\"Bin1S\": %.3f,\"Bin1CalcV\": %.2f}"
def send_confirmation_callback(message, result, user_context):
print ( "IoT Hub responded to message with status: %s" % (result) )
def iothub_client_init():
# Create an IoT Hub client
client = IoTHubClient(CONNECTION_STRING, PROTOCOL)
return client
def iothub_client_telemetry_sample_run():
try:
client = iothub_client_init()
print ( "IoT Hub device sending periodic messages, press Ctrl-C to exit" )
while True:
# Build the message with simulated telemetry values.
b1load = comm.Read('Bin01.Content.Load')
b1grower = comm.Read('Bin01.Content.Grower')
b1variety = comm.Read('Bin01.Content.Variety')
b1statedton = comm.Read('Bin01.Content.Stated_Tn')
b1s = comm.Read('Bin01.Content.SG')
b1calcvol = comm.Read('Bin01.Content.Calc_Vol')
msg_txt_formatted = MSG_TXT % (b1loads, b1growers, b1varietys, b1statedton, b1s, b1calcvol)
message = IoTHubMessage(msg_txt_formatted)```
解决方案:
不要尝试使用字符串格式构建 JSON。创建字典并使用 json.dumps().
我正在尝试使用 IOTHub 遥测从 PLC 读取值并使用 azure 将它们流式传输到 PowerBI。
当我 运行 代码并且能够看到集线器接收消息时,我能够连接到 PLC 并读取值。但是,流给我一个错误 'InputDeserializerError.InvalidData'。我不确定哪里出错了。请查看下面的代码并建议我如何序列化它。当我删除字符串值并且仅删除 运行 整数和浮点数时,流会拾取它。
# Define the JSON message to send to IoT Hub.
MSG_TXT = "{\"Bin1Load\": %s,\"Bin1Grower\": %s,\"Bin1Variety\": %s,\"Bin1StatedTn\": %.2f,\"Bin1S\": %.3f,\"Bin1CalcV\": %.2f}"
def send_confirmation_callback(message, result, user_context):
print ( "IoT Hub responded to message with status: %s" % (result) )
def iothub_client_init():
# Create an IoT Hub client
client = IoTHubClient(CONNECTION_STRING, PROTOCOL)
return client
def iothub_client_telemetry_sample_run():
try:
client = iothub_client_init()
print ( "IoT Hub device sending periodic messages, press Ctrl-C to exit" )
while True:
# Build the message with simulated telemetry values.
b1load = comm.Read('Bin01.Content.Load')
b1grower = comm.Read('Bin01.Content.Grower')
b1variety = comm.Read('Bin01.Content.Variety')
b1statedton = comm.Read('Bin01.Content.Stated_Tn')
b1s = comm.Read('Bin01.Content.SG')
b1calcvol = comm.Read('Bin01.Content.Calc_Vol')
msg_txt_formatted = MSG_TXT % (b1loads, b1growers, b1varietys, b1statedton, b1s, b1calcvol)
message = IoTHubMessage(msg_txt_formatted)```
解决方案:
不要尝试使用字符串格式构建 JSON。创建字典并使用 json.dumps().