Open mapicard opened 3 years ago
Hi @mapicard, Here is a possible implementation.
Try it on NestedJson.
#include <Arduino_JSON.h>
struct Movie {
String title;
String director;
};
Movie movies[] {
{ "Raiders of the Lost Ark", "Ridley Scott" },
{ "Ghostbusters", "Steven Spielberg" },
{ "THX 1138", "George Lucas" }
};
void setup()
{
Serial.begin(9600);
// Wait for Serial or start after 2.5 seconds
for (auto startNow = millis() + 2500; !Serial && millis() < startNow; delay(500));
// The JSON variable to host the JSON array for the movies
JSONVar jsonMovies;
// A simple index to iterate on while creating the JSON array
int idx { 0 };
// Iterate on movies
for (const auto& m : movies) {
// The JSON dictionary hosting the single movie element
JSONVar jm;
jm["title"] = m.title;
jm["director"] = m.director;
// Add the movie to the array
jsonMovies[idx] = jm;
idx++;
}
// The JSON variable hosting the root of the JSON
JSONVar jsonRoot;
// Add the movies array to the "movies" key
jsonRoot["movies"] = jsonMovies;
auto jsonString = JSON.stringify(jsonRoot);
Serial.println(jsonString);
}
void loop()
{
}
Hi, Is it possible to implement an array of objects with this library? If so, could you provide an example of implementing the following
Thank you