arduino-libraries / Arduino_JSON

Official JSON Library for Arduino
GNU Lesser General Public License v2.1
154 stars 60 forks source link

Parsing API Response fails #29

Closed OmegaHawkeye closed 2 years ago

OmegaHawkeye commented 2 years ago

Hello,

I really apprechiate the simplicity of this library compared to ArduinoJson but I have a problem parsing my Array of Objects I'm fetching from an API.

For testing porpuses i copied some of the API response and stored it in a variable like in the examples and this works but as soon as I try fetching directly from the API but I get an undefined parsed JSONVar returned

I even tried checking that I didn't mess up the fetch but that's all good.

My parsing function

void parseJSON(String input) {

  Serial.print("Input: ");Serial.println(input);Serial.println();

  JSONVar myObject = JSON.parse(input);

  if (JSON.typeof(myObject) == "undefined") {
    Serial.println("Parsing input failed!");
    return;
  }

  Serial.print("myObject = ");
  Serial.println(myObject);

  Serial.println();

  for (int i = 0; i < myObject.length(); i++) {
    JSONVar value = myObject[i];

    Serial.print("myObject[");
    Serial.print(i);
    Serial.print("] = ");
    Serial.println(value);

    if (JSON.typeof(value) == "object") {
      JSONVar keys = value.keys();
      for (int j = 0; j < keys.length(); j++) {
        JSONVar key_value = value[keys[j]];
        Serial.print("myObject[");
        Serial.print(keys[j]);
        Serial.print("] = ");
        Serial.println(key_value);
        Serial.println();
      }
    }
    Serial.println();
  }
  Serial.println();
}

and my Output in the serial monitor

Response: [{"username":"Omega","first_name":"someFirstName","last_name":"someLastName","email":"smth@gmail.com","rooms":[]},{"username":"CDiemberger","first_name":"someFirstName","last_name":"someLastName","email":"smth2@gmail.com","rooms":[]}]
Input: [{"username":"Omega","first_name":"someFirstName","last_name":"someLastName","email":"smth@gmail.com","rooms":[]},{"username":"CDiemberger","first_name":"someFirstName","last_name":"someLastName","email":"smth2@gmail.com","rooms":[]}]

Parsing input failed!

and then appearently is my input wrong since I get Parsing input failed.

I really appreachiate your answer already