Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • A ArduinoJson
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 24
    • Issues 24
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 0
    • Merge requests 0
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Benoît Blanchon
  • ArduinoJson
  • Issues
  • #10
Closed
Open
Issue created Aug 03, 2014 by Administrator@rootContributor

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?

Assignee
Assign to
Time tracking