In my Arduino project, I have the following function:
void callback(char* topic, unsigned char* payload, unsigned int length) {
payload[length] = '\0';
Serial.print("Received: ");
Serial.println(reinterpret_cast<char*>(payload));
DynamicJsonDocument doc(1024);
DeserializationError error = deserializeJson(doc, payload);
if (error) {
Serial.print("deserializeJson() failed: ");
Serial.println(error.c_str());
} else {
serializeJsonPretty(doc, Serial);
Serial.printlnln();
int value = doc["value"];
Serial.println(value);
}
}
The JSON is deserialized correctly (based on the error
variable and the serializeJsonPretty
function), but I still get 0 for the value.
This is the function's output:
This is the output it prints:
Received: "{\"value\":1}"
"{\"value\":1}"
0
What can I do to solve this?