does not match "operator=" (operand types are "String" and "void")


Kaiok 2

I'm querying a web page that returns a JSON string using a NodeMCU ESP8266 microcontroller. The response of the web page 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. payload = payload.trim();then i tried but then i got no match for 'operator=' (operand types are 'String' and 'void')so i tried the payload = payload.replace("\n,"");same problem and then i tried payload = String(payload);again and failed. What am I doing wrong?

Chili Doughnuts

Like someone said, trimand replaceno return String. It "returns" voidand the error message tells you that you are trying to assign a void return value to a string. Just let it go payload.trim();. Also applies topayload.replace("\n","");

But payload.trim();same payload.replace("\n","");thing as not doing it. trim()Removing trailing and leading spaces, as described in the documentation, replace("\n","")will also remove all (and only) newlines in the string, even if it's in the middle.

Whitespace usually refers to spaces, newlines, tabs, and some other miscellaneous characters.

Related


Operand Types of Binary AND Operator

username Which type of operator uses the logical AND operator (&)? I need to input AND shorts aand a "number" mfrom 1 to 16 and get another short b. That primitive type must be m? example: ? m = ...; //with 1 <= m <= 16 short a = 2; short b = a & m; The examp

Operand Types of Binary AND Operator

username Which type of operator uses the logical AND operator (&)? I need to input AND shorts aand a "number" mfrom 1 to 16 and get another short b. That primitive type must be m? example: ? m = ...; //with 1 <= m <= 16 short a = 2; short b = a & m; The examp