2

I'm reading a stored JSON file from an ESP8266. I wish to return it as a StaticJsonDocument for further use.

return it at the end of function was done OK, but I need to assign it for document.

#include <ArduinoJson.h>
#include "FS.h"
#define filename "file.json"


StaticJsonDocument<200> doc;    

void createJSON() {
        doc["sensor"] = "gps";
        doc["time"] = 1351824120;

        JsonArray data = doc.createNestedArray("data");
        data.add(48.756080);
        data.add(2.302038);
}




StaticJsonDocument<512> readJSON_file(){
        StaticJsonDocument<512> read_doc;
        File readFile = SPIFFS.open(filename, "r");
        DeserializationError error = deserializeJson(read_doc, readFile);
        if (error)
                Serial.println(F("Failed to read file, using default configuration"));

        serializeJson(read_doc, Serial);

        return read_doc;
}

void setup() {
        Serial.begin(9600);
        if (!SPIFFS.begin()) {
                Serial.println("Failed to mount file system");
        }

serializeJsonPretty(readJSON_file(),Serial);
}

void loop() {
        // not used in this example
}
guyd
  • 1,049
  • 2
  • 26
  • 61

0 Answers0