Problem converting from ArduinoJson array item to char

I'm trying to convert an item from an ArduinoJson array to a char, and wondering if someone can help.

My code:

#include <ArduinoJson.h>
DynamicJsonDocument json_doc(10000); // remember to allow enough size! 

void setup() {
  // put your setup code here, to run once:
  
  Serial.begin(115200);  
  Serial.println("Starting up...");
}

void loop() {

  long seconds;
  float height;
  char event_type;

  String data = "[[1672049760, \"H\", 4.45, \"12/26/2022 02:16\"], [1672067940, \"L\", 2.71, \"12/26/2022 07:19\"], [1672068415, \"s\", 0.0, \"12/26/2022 07:26\"], [1672079929, \"m\", 3.0, \"12/26/2022 10:38\"], [1672085532, \"N\", 0.0, \"12/26/2022 12:12\"], [1672087740, \"H\", 5.62, \"12/26/2022 12:49\"], [1672098434, \"q\", 3.0, \"12/26/2022 15:47\"], [1672102649, \"S\", 0.0, \"12/26/2022 16:57\"], [1672114440, \"L\", -1.11, \"12/26/2022 20:14\"], [1672117441, \"M\", 4.0, \"12/26/2022 21:04\"], [1672139040, \"H\", 4.52, \"12/27/2022 03:04\"], [1672154836, \"s\", 0.0, \"12/27/2022 07:27\"], [1672158480, \"L\", 2.56, \"12/27/2022 08:28\"]]";

  deserializeJson(json_doc, data);  

  JsonArray array = json_doc.as<JsonArray>();
  for(JsonVariant v : array) {

    seconds = v[0].as<signed long>();
    height = v[2].as<float>(); 
    Serial.println(height);

    event_type = v[2].as<signed char>(); 
    Serial.println(event_type);
    
 
  } 
  Serial.println("---"); 
  delay(1000);
} 

Note this line:

event_type = v[2].as();

I'm pretty sure the issue is that a char doesn't support item assignment, but I can't figure out exactly how to cast the value from the ArduinoJson to my char (the variable "event_type").

I don't imagine anyone has an idea?

Which is your purpose?
are you trying to assign a float value to a char variable (unsigned byte)

Maybe you want assign the element n. 1 "H"???
In that case, this is a string with length 1 not a single char.

You should check for a deserialization error:

StaticJsonDocument<1536> doc;



DeserializationError error = deserializeJson(doc, data);

if (error) {
  Serial.print("deserializeJson() failed: ");
  Serial.println(error.c_str());
  return;
}

I think you wanted v[1] instead of v[2] for event_type.

Thaks for that, very useful.

Oops, indeed.

Ultimately I need to evaluate it against a char. For example I need to do:

char test_var = 'S';
if (test_var == 'S') Serial.println("test var is S!");

My updated code is below, note this conditional:

if (event_type == 'H') Serial.println("Event type is H!");

I'm just trying to make that conditional work with the JSON string in my code.

#include <ArduinoJson.h>
DynamicJsonDocument json_doc(10000); // remember to allow enough size! 




void setup() {
  // put your setup code here, to run once:
  
  Serial.begin(115200);  
  Serial.println("Starting up...");


}

void loop() {


  long seconds;
  float height;
  char event_type;

  String data = "[[1672049760, \"H\", 4.45, \"12/26/2022 02:16\"], [1672067940, \"L\", 2.71, \"12/26/2022 07:19\"] ]";

  deserializeJson(json_doc, data);  

  DeserializationError error = deserializeJson(json_doc, data);
    
  if (error) {
    Serial.print("deserializeJson() failed: ");
    Serial.println(error.c_str());
  }
  

  JsonArray array = json_doc.as<JsonArray>();
  for(JsonVariant v : array) {

    seconds = v[0].as<signed long>();
    height = v[2].as<float>(); 
    Serial.println(height);

    event_type = v[1].as<signed char>(); 
    Serial.println(event_type);

    if (event_type == 'H') Serial.println("Event type is H!");
    else if (event_type == 'L') Serial.println("Event type is L!");

  } 

  char test_var = 'S';
  if (test_var == 'S') Serial.println("test var is S!");
  
  Serial.println("---"); 
  delay(1000);
} 

You can't deserialize a string variable to a char!
Try in this way:

    const char* event_type = v[1];
    Serial.println(event_type);

    if (event_type[0] == 'H') Serial.println("Event type is H!");
    else if (event_type[0] == 'L') Serial.println("Event type is L!");

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.