Skip to main content
deleted 206 characters in body; edited title
Source Link
dda
  • 1.6k
  • 1
  • 12
  • 18

Arduino Json ForeachJSON foreach loop

I'm trying to extract values using arduinojsonArduinoJson. The Valuesvalues aren't fixed therefore, therefore I need to use the foreach loop.

I'm trying this, but I can't seem to use the values Likelike I could with.

root["0"]["value"].as<const char*>();

Here is my code.

 void loop(){
    int httpCode = http.GET();
  if(httpCode > 0) {
    // if Get request has processed
    if(httpCode == HTTP_CODE_OK) {
          payload = http.getString();
      }
      if(payload.length() > 0) {
          Serial.println(payload);
          JsonObject& root = jsonBuffer.parseObject(payload);
          if (!root.success()) {
            Serial.println("parseObject() failed");
            jsonBuffer.clear();
          } else {
            Serial.println("Data Fetched");
        for (JsonPair& node : root) {
            Serial.print("0 State: ");
            Serial.println(*node);
//                Serial.print("0 Value: ");
//                Serial.println(root["0"]["value"].as<const char*>());
              }
            http.end();
        }
    } else {
      Serial.println("Payload Empty");
    }
  }
 
}

Please avoid any missing brackets,. Thanks in advance.

[{"state": true, "value": 10}, {"state": false, "value": 8}, {"state": false, "value": 5}, {"state": false, "value": 13}]

Arduino Json Foreach loop

I'm trying to extract values using arduinojson. The Values aren't fixed therefore, I need to use the foreach loop.

I'm trying this, but I can't seem to use the values Like I could with.

root["0"]["value"].as<const char*>();

Here is my code.

 void loop(){
    int httpCode = http.GET();
  if(httpCode > 0) {
    // if Get request has processed
    if(httpCode == HTTP_CODE_OK) {
          payload = http.getString();
      }
      if(payload.length() > 0){
          Serial.println(payload);
          JsonObject& root = jsonBuffer.parseObject(payload);
          if (!root.success()) {
            Serial.println("parseObject() failed");
            jsonBuffer.clear();
          }else{
            Serial.println("Data Fetched");
        for (JsonPair& node : root){
            Serial.print("0 State: ");
            Serial.println(*node);
//                Serial.print("0 Value: ");
//                Serial.println(root["0"]["value"].as<const char*>());
              }
            http.end();
        }
    }else{
      Serial.println("Payload Empty");
    }
  }
 
}

Please avoid any missing brackets, Thanks in advance.

[{"state": true, "value": 10}, {"state": false, "value": 8}, {"state": false, "value": 5}, {"state": false, "value": 13}]

Arduino JSON foreach loop

I'm trying to extract values using ArduinoJson. The values aren't fixed, therefore I need to use the foreach loop.

I'm trying this, but I can't seem to use the values like I could with.

root["0"]["value"].as<const char*>();

Here is my code.

void loop(){
  int httpCode = http.GET();
  if(httpCode > 0) {
    // if Get request has processed
    if(httpCode == HTTP_CODE_OK) {
      payload = http.getString();
    }
    if(payload.length() > 0) {
      Serial.println(payload);
      JsonObject& root = jsonBuffer.parseObject(payload);
      if (!root.success()) {
        Serial.println("parseObject() failed");
        jsonBuffer.clear();
      } else {
        Serial.println("Data Fetched");
        for(JsonPair& node : root) {
          Serial.print("0 State: ");
          Serial.println(*node);
        }
        http.end();
      }
    } else {
      Serial.println("Payload Empty");
    }
  }
}

Please avoid any missing brackets. Thanks in advance.

[{"state": true, "value": 10}, {"state": false, "value": 8}, {"state": false, "value": 5}, {"state": false, "value": 13}]
Added Plausible json
Source Link

I'm trying to extract values using arduinojson. The Values aren't fixed therefore, I need to use the foreach loop.

I'm trying this, but I can't seem to use the values Like I could with.

root["0"]["value"].as<const char*>();

Here is my code.

 void loop(){
    int httpCode = http.GET();
  if(httpCode > 0) {
    // if Get request has processed
    if(httpCode == HTTP_CODE_OK) {
          payload = http.getString();
      }
      if(payload.length() > 0){
          Serial.println(payload);
          JsonObject& root = jsonBuffer.parseObject(payload);
          if (!root.success()) {
            Serial.println("parseObject() failed");
            jsonBuffer.clear();
          }else{
            Serial.println("Data Fetched");
        for (JsonPair& node : root){
            Serial.print("0 State: ");
            Serial.println(*node);
//                Serial.print("0 Value: ");
//                Serial.println(root["0"]["value"].as<const char*>());
              }
            http.end();
        }
    }else{
      Serial.println("Payload Empty");
    }
  }

}

Please avoid any missing brackets, Thanks in advance.

[{"state": true, "value": 10}, {"state": false, "value": 8}, {"state": false, "value": 5}, {"state": false, "value": 13}]

I'm trying to extract values using arduinojson. The Values aren't fixed therefore, I need to use the foreach loop.

I'm trying this, but I can't seem to use the values Like I could with.

root["0"]["value"].as<const char*>();

Here is my code.

 void loop(){
    int httpCode = http.GET();
  if(httpCode > 0) {
    // if Get request has processed
    if(httpCode == HTTP_CODE_OK) {
          payload = http.getString();
      }
      if(payload.length() > 0){
          Serial.println(payload);
          JsonObject& root = jsonBuffer.parseObject(payload);
          if (!root.success()) {
            Serial.println("parseObject() failed");
            jsonBuffer.clear();
          }else{
            Serial.println("Data Fetched");
        for (JsonPair& node : root){
            Serial.print("0 State: ");
            Serial.println(*node);
//                Serial.print("0 Value: ");
//                Serial.println(root["0"]["value"].as<const char*>());
              }
            http.end();
        }
    }else{
      Serial.println("Payload Empty");
    }
  }

}

Please avoid any missing brackets, Thanks in advance.

I'm trying to extract values using arduinojson. The Values aren't fixed therefore, I need to use the foreach loop.

I'm trying this, but I can't seem to use the values Like I could with.

root["0"]["value"].as<const char*>();

Here is my code.

 void loop(){
    int httpCode = http.GET();
  if(httpCode > 0) {
    // if Get request has processed
    if(httpCode == HTTP_CODE_OK) {
          payload = http.getString();
      }
      if(payload.length() > 0){
          Serial.println(payload);
          JsonObject& root = jsonBuffer.parseObject(payload);
          if (!root.success()) {
            Serial.println("parseObject() failed");
            jsonBuffer.clear();
          }else{
            Serial.println("Data Fetched");
        for (JsonPair& node : root){
            Serial.print("0 State: ");
            Serial.println(*node);
//                Serial.print("0 Value: ");
//                Serial.println(root["0"]["value"].as<const char*>());
              }
            http.end();
        }
    }else{
      Serial.println("Payload Empty");
    }
  }

}

Please avoid any missing brackets, Thanks in advance.

[{"state": true, "value": 10}, {"state": false, "value": 8}, {"state": false, "value": 5}, {"state": false, "value": 13}]
Source Link

Arduino Json Foreach loop

I'm trying to extract values using arduinojson. The Values aren't fixed therefore, I need to use the foreach loop.

I'm trying this, but I can't seem to use the values Like I could with.

root["0"]["value"].as<const char*>();

Here is my code.

 void loop(){
    int httpCode = http.GET();
  if(httpCode > 0) {
    // if Get request has processed
    if(httpCode == HTTP_CODE_OK) {
          payload = http.getString();
      }
      if(payload.length() > 0){
          Serial.println(payload);
          JsonObject& root = jsonBuffer.parseObject(payload);
          if (!root.success()) {
            Serial.println("parseObject() failed");
            jsonBuffer.clear();
          }else{
            Serial.println("Data Fetched");
        for (JsonPair& node : root){
            Serial.print("0 State: ");
            Serial.println(*node);
//                Serial.print("0 Value: ");
//                Serial.println(root["0"]["value"].as<const char*>());
              }
            http.end();
        }
    }else{
      Serial.println("Payload Empty");
    }
  }

}

Please avoid any missing brackets, Thanks in advance.