bblanchon / ArduinoJson

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

I am not able to make IF condition #2002

Closed Regdr closed 10 months ago

Regdr commented 10 months ago

Hello I am trying to make a condition from a value taken from http://......./ test.json usually I put all the connection settings and print immediately on tft, but now I need to make a condition depending of the value received

part of the code:

//JSON HTTP_REQUEST
  WiFiClient client;
  HTTPClient http;  
// Send request
  http.useHTTP10(true);
  http.begin(client, "http://........../2CF432C2560E.json");
  http.GET();
// Parse response
  DynamicJsonDocument doc(2048);
  deserializeJson(doc, http.getStream());
// Read values
  tft.setTextSize(1);
  tft.setCursor(250,45);
  tft.setTextColor(TFT_BLUE, TFT_BLACK);
  tft.println(doc["T.cpu"].as<float>(), 1); 

example:

  if (doc["switch.fan"].as<const char*>() == on) {
    tft.println("X");
 }

my intention is to print some simbol if the condition of the switch is ON, many thanks

bblanchon commented 10 months ago

Hi @Regdr,

Assuming your input looks like this:

{
  "switch": {
    "fan": "on"
  }
}

Then, you can use the following:

if (doc["switch"]["fan"] == "on")

Best regards, Benoit

Regdr commented 10 months ago

Thanks @bblanchon is working

bblanchon commented 10 months ago

You're welcome, @Regdr. Thank you for using ArduinoJson! Don't forget to cast a star to support the project :wink: