bblanchon / ArduinoJson

📟 JSON library for Arduino and embedded C++. Simple and efficient.
https://arduinojson.org
MIT License
6.71k stars 1.12k forks source link

Parsing failed. I dont know why #613

Closed hansfeenstra1997 closed 6 years ago

hansfeenstra1997 commented 6 years ago

include

include

include

include

include

define USE_SERIAL Serial

ESP8266WiFiMulti WiFiMulti;

void setup() {

USE_SERIAL.begin(115200);

USE_SERIAL.println();
USE_SERIAL.println();
USE_SERIAL.println();

pinMode(2, OUTPUT);
digitalWrite(2, LOW);    

for(uint8_t t = 4; t > 0; t--) {
    USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
    USE_SERIAL.flush();
    delay(1000);
}

WiFiMulti.addAP("username", "password");

}

void loop() {

// wait for WiFi connection
if((WiFiMulti.run() == WL_CONNECTED)) {

    HTTPClient http;        
    http.begin("http://192.168.1.72/test.html"); //HTTP

    USE_SERIAL.print("[HTTP] GET...\n");
    int httpCode = http.GET();

    if(httpCode > 0) {

        USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);

        if(httpCode == HTTP_CODE_OK) {
            //String payload = http.getString();

            const size_t BUFFER_SIZE = JSON_OBJECT_SIZE(3) + JSON_ARRAY_SIZE(2) + 60;
            DynamicJsonBuffer jsonBuffer(BUFFER_SIZE);

            Serial.println(http.getString());
            JsonObject& root = jsonBuffer.parseObject(http.getString());

            if (!root.success()) {
            Serial.println("parseObject() failed");
          } else{
            int switch_id = root["switch_id"];
            int status = root["status"];
            USE_SERIAL.printf("Switch ID: %d\n", switch_id);
            USE_SERIAL.printf("Status: %d\n", status);
          }

            //USE_SERIAL.println(payload);
        }
    } else {
        USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
    }
    http.end();
}

delay(10000);

}

Input json string from get: {switch_id":1, "status":1}

bblanchon commented 6 years ago

Hi,

It is very likely to be caused by a wrong input.

I'm not familiar with ESP8266HTTPClient. Does the result of http.getString() include the headers? Are you sure that Chunked Transfer Encoding is disabled or handled by the library?

Please check Why parsing fails?, your answer is there.

Regards, Benoit

hansfeenstra1997 commented 6 years ago

Hello,

The result of http.getString() does not include headers. It contains the result from the get request: "{switch_id":1, "status":1}".

Chunked transfer encoding is handled by the library. I searched on the "Why parsing fails" page. And I can't find the solution for this.

Regards, Hans

bblanchon commented 6 years ago

This is not a valid JSON document, a quote is missing

{switch_id":1, "status":1}"

The code works after adding the missing quote. Demo: https://wandbox.org/permlink/jjgyAWUEZwcgseGJ

hansfeenstra1997 commented 6 years ago

I see, thank you!

Thanks for your support.

Can i put my code as an example on your page. I work with a WiFi module, maybe other people also want to try it. For a combination for IOT.

bblanchon commented 6 years ago

@hansfeenstra1997, feel free to add a link to your project in the wiki