Can't fill JsonArray in for loop
Created by: franklinvv
(Disclaimer: I'm not at all an expert in C/C++)
I am trying to build a JsonArray in a for loop on an Arduino Mega 2560, iterating through an array. Here is a (somewhat) minimal example of what I'm trying to do.
struct Person {
int id;
char name[32];
};
Person boss;
boss.id = 1;
strcpy(boss.name, "Jeff");
Person employee;
employee.id = 2;
strcpy(employee.name, "John");
Person persons[2] = {boss, employee};
JsonArray<2> json;
for(int i = 0; i < 2; i++) {
JsonObject<2> object;
object.add("id", persons[i].id);
object.add("name", persons[i].name);
json.add(object);
}
Serial.print(json);
The expected result is:
[
{
id: 1,
name: "Jeff"
},
{
id: 2,
name: "John"
}
]
However, I get this:
[
{
id: 2,
name: "John"
},
{
id: 2,
name: "John"
}
]
Any idea why I can't construct an JsonArray this way?