Skip to content

Problem with nested objects #497

@bcatalin

Description

@bcatalin

Hi,

I have a issue reading (or writing) a nested JSON on an ESP8266.
What I try to have is a file is [ {"m":1, "h":2} , {"m":10, "h":20} , {"m":100, "h":200} ]

I am using SPIFFS to write the JSON to a file then to read it.

For one object is working fine, but for nested array does not working at all.

Code below:

Not working case:

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ArduinoJson.h>
#include "FS.h"

void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
delay(2000);
int i=0;
if( SPIFFS.begin() )
{
File my_file = SPIFFS.open("xfile.json", "w");
if (!my_file) {
Serial.println("failed to open xfile.json file for writing :-(");
return; //==============>
}
StaticJsonBuffer<300> jsonBuffer;
JsonArray& my_array = jsonBuffer.createArray();
for(i=0;i<3;i++)
{
JsonObject& my_entry = my_array.createNestedObject();
my_entry["m"] = 20;
my_entry["h"] = 14;
}
my_array.printTo(Serial);
Serial.println("");
my_array.printTo(my_file);
my_file.close();
delay(1000);
//---------------------------- READ ----------------------------------
File myFile = SPIFFS.open("xfile.json", "r");
if (myFile)
{
Serial.println("opened xfile.json file");
size_t size = myFile.size();
// Allocate a buffer to store contents of the file.
std::unique_ptr<char[]> buf(new char[size]);
Serial.println(size);
Serial.println(buf.get());
myFile.readBytes(buf.get(), size);
DynamicJsonBuffer jsonBuffer;
//StaticJsonBuffer<200> jsonBuffer;
JsonObject& jsona = jsonBuffer.parseObject(buf.get());
jsona.printTo(Serial);
Serial.println("");
if (jsona.success())
{
Serial.println("READ OK");
}
else
{
Serial.println("Json invalid");
}
}
myFile.close();
}//end SPIFFS.begin
}//end setup
void loop() {
// put your main code here, to run repeatedly:
}

and working case:

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ArduinoJson.h>
#include "FS.h"

void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
delay(2000);
int i=0;
if( SPIFFS.begin() )
{
File my_file = SPIFFS.open("xfile.json", "w");
if (!my_file) {
Serial.println("failed to open xfile.json file for writing :-(");
return; //==============>
}

StaticJsonBuffer<300> jsonBuffer;
JsonObject& my_entry = jsonBuffer.createObject();
my_entry["m"] = 20;
my_entry["h"] = 14;

my_entry.printTo(Serial); 
Serial.println("");
my_entry.printTo(my_file);
my_file.close();

delay(1000);
//---------------------------- READ ----------------------------------

File myFile = SPIFFS.open("xfile.json", "r");
if (myFile) 
{
    Serial.println("opened xfile.json file");
    size_t size = myFile.size();
    // Allocate a buffer to store contents of the file.
    std::unique_ptr<char[]> buf(new char[size]);
    Serial.println(size);
    Serial.println(buf.get());
    myFile.readBytes(buf.get(), size);
    DynamicJsonBuffer jsonBuffer;
    //StaticJsonBuffer<200> jsonBuffer;        
    JsonObject& jsona = jsonBuffer.parseObject(buf.get());
    jsona.printTo(Serial);
    Serial.println("");
    if (jsona.success()) 
    { 
      Serial.println("READ OK");
    }
    else
    {
      Serial.println("Json invalid");
    }
}
myFile.close();

}//end SPIFFS.begin
}//end setup

void loop() {
// put your main code here, to run repeatedly:

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    v5ArduinoJson 5

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy