Deserialize String into StaticJsonDocument

875 views Asked by At

I'm using ArduinoJson, HTTPClient Libraries for my HTTP related communications in ESP8266. I have used a POST method API which returns me a String (but it's a json).

void configure() {
    String jsonBody;

    const String dataFetchUrl = "http://192.168.1.8:3334/api/details";

    StaticJsonDocument<800> JSONDoc;

    JSONDoc["imei"] = "2321";
    JSONDoc["email"] = "[email protected]";


    serializeJson(JSONDoc, jsonBody);

    int httpCode = http.POST(jsonBody);

    if (httpCode > 0) {
        const String payload = http.getString(); // returns the json
        sendPayload(payload);
    }
}

void sendPayload(String payload) {
    StaticJsonDocument<1000> PayloadDoc;
    DeserializationError error = deserializeJson(PayloadDoc, payload);

    if (error) {
        Serial.println("Deserialization error occurred!");
        return;
    }

    String gender = PayloadDoc["gender"];
    Serial.printf("Gender: %s", gender);
}

This always returns the deserialization error. what was your recommendation?

0

There are 0 answers