-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Closed
Labels
Description
I am using the ArduinoJSON library with LittlesFS and have used it on quite a few projects successfully. However never quite like this. I have int arrays, long arrays and a char array. Please see the code below. Can any one help me with the syntax for the char array? Please see the commented line in the code.
#include <ArduinoJson.h>
#include "FS.h"
#include <LittleFS.h>
long cardUIDlittleFS[3] = {123456789,989748741,326561145};
char* cardHolder[3] = {"David","Sam","Doug"};
int cardActive[3] = {1,0,1};
long cardUIDlittleFS1[3];
char* cardHolder1[3];
int cardActive1[3];
bool loadData() {
File dataFile = LittleFS.open("/data.json", "r");
if (!dataFile) {
Serial.println("Failed to open data file");
return false;
}
size_t size = dataFile.size();
if (size > 1024) {
Serial.println("Data file size is too large");
return false;
}
std::unique_ptr<char[]> buf(new char[size]);
dataFile.readBytes(buf.get(), size);
StaticJsonDocument<300> doc;
auto error = deserializeJson(doc, buf.get());
if (error) {
Serial.println("Failed to parse config file");
return false;
}
for(int i=0; i<3; ++i)
{
cardUIDlittleFS1[i] = doc["CardUID"][i];
}
for(int i=0; i<3; ++i)
{
cardHolder1[i] = doc["CardHLDR"][i];
}
return true;
}
bool saveData() {
StaticJsonDocument<300> doc;
JsonArray CardUID = doc.createNestedArray("CardUID");
JsonArray CardHLDR = doc.createNestedArray char*("CardHLDR"); /////want to put char array into JSON doc and loop it in and out
for(int i=0; i<3; ++i)
{
CardUID.add(cardUIDlittleFS[i]);
Serial.println(cardUIDlittleFS[i]);
}
for(int i=0; i<3; ++i)
{
CardHLDR[i]=cardHolder[i];
Serial.println(cardHolder[i]);
}
File dataFile = LittleFS.open("/data.json", "w");
if (!dataFile) {
Serial.println("Failed to open config file for writing");
return false;
}
serializeJson(doc, dataFile);
return true;
}
void setup() {
Serial.begin(74880);
if (!LittleFS.begin()) {
Serial.println("Failed to mount file system");
return;
}
if (!loadData())
{
Serial.println("Failed to load Data");
}
else
{
Serial.println("Data loaded");
}
delay(5000);
saveData();
Serial.println("Print");
for(int i=0; i<3; ++i)
{
Serial.println(cardUIDlittleFS1[i]);
}
Serial.println("name");
for(int i=0; i<3; ++i)
{
Serial.println(cardHolder1[i]);
}
}
void loop() {
}
PS not sure how to mark this as a question..