Skip to content

How can I use a Struct to return JsonArray from a function ? #610

@gusantor

Description

@gusantor

Thank you for your library

I'm trying to bring data from json files, repeteadly. To do so, I want to build a function which receive a file name and return a Struct, with a jsonArray among others

At jsonStruct, if I not declare puntos as reference, receive this error message: use of deleted function 'jsonStruct::jsonStruct()', which leads me to #290, but this conversation surpases my knowledge

if I declare puntos (inside struct) as reference, I receive error message: error: invalid conversion from 'ArduinoJson::Internals::JsonVariantAs<char*>::type {aka const char*}' to 'char*' [-fpermissive]
return as();

So my question is how can I use a Struct to return JsonArray from a function ?, thank you in advance

//the struct declared globally
struct jsonStruct {
  char *nombre;
  char *titulo;
  char *descripcion;
  JsonArray *puntos;//<-- not declared as reference leads me to #290
};

//Then I call the function from here
void handleLista(AsyncWebServerRequest *request){

  jsonStruct jsonData;
  jsonData = traeJsonFile("/lista-pares-biomagneticos.json");

  request->send(200, "text/html", jsonData.nombre);

}

//and the function to be called many times (for each file)
jsonStruct traeJsonFile(char *fileName){
  File file = SPIFFS.open(fileName, "r");

  jsonStruct fileData;

  if(file){
    size_t size = file.size();
    std::unique_ptr<char[]> buf (new char[size]);

    file.readBytes(buf.get(), size);

    JsonObject& root = jsonBuffer.parseObject(buf.get());

    if (root.success()) {
        fileData.nombre = root["nombre"];//<-- error is generated here
            //fileData.nombre = "test";//<-- no error
            .
            .
            .I should continue populating fileData with JsonArray and others
    }
    }
    return fileData;
}

EDIT
sorry me by huge delay()

I've tried your code but received error at first try ... didn't have the time to try some variants ... sorry me

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions