Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Key and value is present but returns NULL #1273

Closed
vaz83 opened this issue May 19, 2020 · 3 comments
Closed

Key and value is present but returns NULL #1273

vaz83 opened this issue May 19, 2020 · 3 comments
Labels
question v6 ArduinoJson 6

Comments

@vaz83
Copy link

vaz83 commented May 19, 2020

Hi,
I am facing a strange problem. I saved a JSON document into the ESP8266 file system.

Then i load it using this code:
` DynamicJsonDocument jDocLoad(1500);
DynamicJsonDocument jDoc(1500);

if (LittleFS.begin())
{
if (LittleFS.exists("/topRecordings.json"))
{
File cFile = LittleFS.open("/topRecordings.json", "r");
if (cFile)
{
size_t size = cFile.size();
// Allocate a buffer to store contents of the file.
std::unique_ptr<char[]> buf(new char[size]);
cFile.readBytes(buf.get(), size);
DeserializationError err = deserializeJson(jDocLoad,buf.get());
Serial.println(err.c_str());
serializeJsonPretty(jDocLoad,Serial);
}
cFile.close();
}`

The output of the serializeJsonPretty is this:

{
"maxTempC": 26.54,
"maxTempF": 79.772,
"maxTempTimeStampUTC": "2020-05-19T15:51:44",
"maxTempUNIXTime": 1589903504
}

After, i try to get the maxTempC value, but gives no value and when i put the line jDocLoad.containsKey("maxTempC") it returns FALSE, while the key and value is there which can be seen on the serializeJSonPretty.

What can be the problem?

@ewaldc
Copy link

ewaldc commented May 19, 2020

You can deserialize directly from a stream:
DeserializationError err = deserializeJson(jDocLoad, cFile);
Less lines of code and less memory used...

What is your problem exactly ? If serializeJsonPretty returns the correct Json, than it means you have no error from deserializeJson and thus all is read fine from LittleFS. In that case, the problem is not in the code above. Or do I understand this wrong?

@vaz83
Copy link
Author

vaz83 commented May 19, 2020

It returns the correct code, but right after i do this:

jDocLoad.containsKey("maxTempC") which returns false.. it's only the first field, i don't understand why..

@vaz83
Copy link
Author

vaz83 commented May 19, 2020

Deserialize directly from the stream solved the problem! :) thanks

@vaz83 vaz83 closed this as completed May 19, 2020
@lock lock bot locked as resolved and limited conversation to collaborators Jun 24, 2020
@bblanchon bblanchon added the v6 ArduinoJson 6 label Feb 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
question v6 ArduinoJson 6
Projects
None yet
Development

No branches or pull requests

3 participants