kaioker2
kaioker2

Reputation: 329

No match for 'operator=' (operand types are 'String' and 'void')

I am querying a webpage that returns a JSON string using a NodeMCU ESP8266 microcontroller. Response from the webpage looks like this:

{"1":true,"2":false,"3":false,"4":true,"5":true,"6":false,"7":false,"8":false}

The code I am using looks like this:

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
String payload = "";
const char* ssid = "ssid";
const char* password = "password";
String url = "example.com/data.json";
void setup() {
  Serial.begin(115200);
  delay(2000); while (!Serial);

  WiFi.begin(ssid, password);
  while (WiFi.status() !=WL_CONNECTED){
    delay(500);
    Serial.print(".");
  }
  Serial.println(WiFi.localIP());
  pinMode(D0, OUTPUT);
}
void loop() {
  StaticJsonBuffer<100> jsonBuffer;
  delay(5000);
  HTTPClient http;
  http.begin(url);
  int httpCode = http.GET();
  Serial.println(httpCode);
  Serial.println(http.getString());
  if (httpCode > 0) {
    payload = http.getString();
  }
  http.end();
  JsonObject& root = jsonBuffer.parseObject(payload);
  Serial.println(payload);
  if(!root.success()) {
    Serial.println("parseObject() failed");
  }
  if(root["1"] == true) {
    Serial.println("true");
  }
  digitalWrite(D0, !digitalRead(D0));
}

I believe the reason it fails to parse is that the payload variable ends with a trailing newline character. I then attempted payload = payload.trim(); but then I get no match for 'operator=' (operand types are 'String' and 'void') so I tried payload = payload.replace("\n,""); same issue, then I tried payload = String(payload); again, failure. What am I doing wrong?

Upvotes: 0

Views: 9028

Answers (1)

ChilliDoughnuts
ChilliDoughnuts

Reputation: 377

Like someone has said, trim and replace do not return String. It "returns" void, the error message is telling you that you're trying to assign a void return to a String. It suffices to put payload.trim();. The same goes for payload.replace("\n","");

But payload.trim(); and payload.replace("\n",""); don't do the same thing. As mentioned in the docs, trim() removes trailing and leading whitespaces, while replace("\n","") will remove all (and only) newlines in the string, even if it's in the middle.

Whitespace generally refers to space, newline, tabs, and some other miscellaneous characters.

Upvotes: 4

Related Questions