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

How to decode arrays in array? #400

Closed
kiralikbeyin opened this issue Dec 21, 2016 · 5 comments
Closed

How to decode arrays in array? #400

kiralikbeyin opened this issue Dec 21, 2016 · 5 comments
Labels
question v5 ArduinoJson 5

Comments

@kiralikbeyin
Copy link

String tester = "[["🕐","Pt","Sa","Çr","Pe","Cm","Ct","Pz"],["00-01","21","21","21","21","21","21","21"],["01-02","21","21","21","21","21","21","21"]]";

Couldn't find an example, is it possible?

@bblanchon
Copy link
Owner

JsonArray& root = jsonBuffer.parseArray(tester);
const char* value = root[0][0];

@kiralikbeyin
Copy link
Author

kiralikbeyin commented Dec 21, 2016

@bblanchon Thank you but i changed it because i get empty responses (Half of this Json data works)

Tried 1024-2048-4096 Staticbuffer but didn't work. (Nodemcu 1.0)
Latest ArduinoJson lib installed

[{"Saat":"00-01","Pt":"21","Sa":"21","Çr":"21","Pe":"21","Cm":"21","Ct":"21","Pz":"21"},{"Saat":"01-02","Pt":"21","Sa":"21","Çr":"21","Pe":"21","Cm":"21","Ct":"21","Pz":"21"},{"Saat":"02-03","Pt":"21","Sa":"21","Çr":"21","Pe":"21","Cm":"21","Ct":"21","Pz":"21"},{"Saat":"03-04","Pt":"21","Sa":"21","Çr":"21","Pe":"21","Cm":"21","Ct":"21","Pz":"21"},{"Saat":"04-05","Pt":"21","Sa":"21","Çr":"21","Pe":"21","Cm":"21","Ct":"21","Pz":"21"},{"Saat":"05-06","Pt":"21","Sa":"21","Çr":"21","Pe":"21","Cm":"21","Ct":"21","Pz":"21"},{"Saat":"06-07","Pt":"21","Sa":"21","Çr":"21","Pe":"21","Cm":"21","Ct":"21","Pz":"21"},{"Saat":"07-08","Pt":"21","Sa":"21","Çr":"21","Pe":"21","Cm":"21","Ct":"21","Pz":"21"},{"Saat":"08-09","Pt":"21","Sa":"21","Çr":"21","Pe":"21","Cm":"21","Ct":"21","Pz":"21"},{"Saat":"09-10","Pt":"21","Sa":"21","Çr":"21","Pe":"21","Cm":"21","Ct":"21","Pz":"21"},{"Saat":"10-11","Pt":"21","Sa":"21","Çr":"21","Pe":"21","Cm":"21","Ct":"21","Pz":"21"},{"Saat":"11-12","Pt":"21","Sa":"21","Çr":"21","Pe":"21","Cm":"21","Ct":"21","Pz":"21"},{"Saat":"12-13","Pt":"21","Sa":"21","Çr":"21","Pe":"21","Cm":"21","Ct":"21","Pz":"21"},{"Saat":"13-14","Pt":"21","Sa":"21","Çr":"21","Pe":"21","Cm":"21","Ct":"21","Pz":"21"},{"Saat":"14-15","Pt":"21","Sa":"21","Çr":"21","Pe":"21","Cm":"21","Ct":"21","Pz":"21"},{"Saat":"15-16","Pt":"21","Sa":"21","Çr":"21","Pe":"21","Cm":"21","Ct":"21","Pz":"21"},{"Saat":"16-17","Pt":"21","Sa":"21","Çr":"21","Pe":"21","Cm":"21","Ct":"21","Pz":"21"},{"Saat":"17-18","Pt":"21","Sa":"21","Çr":"21","Pe":"21","Cm":"21","Ct":"21","Pz":"21"},{"Saat":"18-19","Pt":"21","Sa":"21","Çr":"21","Pe":"21","Cm":"21","Ct":"21","Pz":"21"},{"Saat":"19-20","Pt":"21","Sa":"21","Çr":"21","Pe":"21","Cm":"21","Ct":"21","Pz":"21"},{"Saat":"20-21","Pt":"21","Sa":"21","Çr":"21","Pe":"21","Cm":"21","Ct":"21","Pz":"21"},{"Saat":"21-22","Pt":"21","Sa":"21","Çr":"21","Pe":"21","Cm":"21","Ct":"21","Pz":"21"},{"Saat":"22-23","Pt":"21","Sa":"21","Çr":"21","Pe":"21","Cm":"21","Ct":"21","Pz":"21"},{"Saat":"23-00","Pt":"21","Sa":"21","Çr":"21","Pe":"21","Cm":"21","Ct":"21","Pz":"21"}]

int haft[23][8];

File haftal = SPIFFS.open("/haftal.json", "r");
if (haftal) {
  size_t size = haftal.size();
  std::unique_ptr<char[]> bufhaft(new char[size]);
  haftal.readBytes(bufhaft.get(), size);
  StaticJsonBuffer<4096> jsonBufferhaft;
  JsonArray& jsonhaft = jsonBufferhaft.parseArray(bufhaft.get());
  haftal.close();
  if (!jsonhaft.success())
  {
    Serial.println("parseObject() failed");
    return;
  }
  else
  {
// I can enter here
    Serial.println("parseObject() OKOKOKOKOK");
    String denet;
    jsonhaft.printTo(denet);
    haftalikws =  denet; //carry to a global String

    for (int i = 0 ; i < 25; i++) {
      haft[i][1] = jsonhaft[i]["Pt"];
      haft[i][2] = jsonhaft[i]["Sa"];
      haft[i][3] = jsonhaft[i]["Çr"];
      haft[i][4] = jsonhaft[i]["Pe"];
      haft[i][5] = jsonhaft[i]["Cm"];
      haft[i][6] = jsonhaft[i]["Ct"];
      haft[i][7] = jsonhaft[i]["Pz"];
    }
  }
}

@bblanchon
Copy link
Owner

bblanchon commented Dec 22, 2016

The ArduinoJson Assistant says that you need 3560 bytes to store the object tree, so a JsonBuffer of 4096 bytes is appropriate.

But it's way too big for the stack of an ESP8266; you need to move that memory to the heap.

To do this, just replace:

StaticJsonBuffer<4096> jsonBufferhaft;

by:

DynamicJsonBuffer jsonBufferhaft(4096);

Also, I see that the haft array is using a significant part of the stack, you may need to move it to the heap too.

By the way, I see that you switch from String to char[] which is a good idea, since it avoids the duplication of the JSON input in the JsonBuffer 👍

One last thing, you could get a significant performance boost by using iterators:

for (JsonArray& arr : jsonhaft) {
  haft[i][1] = arr["Pt"];
  haft[i][2] = arr["Sa"];
  haft[i][3] = arr["Çr"];
  haft[i][4] = arr["Pe"];
  haft[i][5] = arr["Cm"];
  haft[i][6] = arr["Ct"];
  haft[i][7] = arr["Pz"];
  i++;
}

@kiralikbeyin
Copy link
Author

Thank you it works fine.

I didnt understand this because of [i]

for (JsonArray& arr : jsonhaft) {
  haft[i][1] = arr["Pt"];
  haft[i][2] = arr["Sa"];
  haft[i][3] = arr["Çr"];
  haft[i][4] = arr["Pe"];
  haft[i][5] = arr["Cm"];
  haft[i][6] = arr["Ct"];
  haft[i][7] = arr["Pz"];
  i++;
}

@bblanchon
Copy link
Owner

You're welcome @kiralikbeyin, thanks for using ArduinoJson.

Don't hesitate to edit the wiki if you think you can improve it.
In particular, feel free to add your projects to Projects using ArduinoJson.

Also, a GitHub star is always appreciated 😉

Repository owner locked and limited conversation to collaborators Sep 21, 2018
@bblanchon bblanchon added the v5 ArduinoJson 5 label Feb 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
question v5 ArduinoJson 5
Projects
None yet
Development

No branches or pull requests

2 participants