manish manish - 3 months ago 28
Java Question

How to get Object from MQTT message payload

On receiving mqtt message I am trying to get Object from payload (byte[])

Object ob = SerializationUtils.deserialize(message.getPayload());


I am getting:

org.apache.commons.lang.SerializationException: java.io.StreamCorruptedException: invalid stream header: 7B22736E
at org.apache.commons.lang.SerializationUtils.deserialize(SerializationUtils.java:168)
at org.apache.commons.lang.SerializationUtils.deserialize(SerializationUtils.java:193)

Answer

You need a JSON parser not the Apache Commons Serialization library.

Something like the JSON.org parser

JSONObject obj = new JSONObject(msg.toString());