Skip to content

Encoding variable number of nested objects #57

@orphuz

Description

@orphuz

Hello and thank you for your great work.
I use your library for building a multisensor platform (based on an Arduino Mega) for indoor air quality and use JSON for communicating with an handheld device. Your library does quite a job in my project. However I still have one problem for which i just can't find a proper solution.

My idea is generate a variable number of nested object within an iteration. In this iteration I try to assign the values of an array of integers and an corresponding array of chararrays to the key-value pairs of each object. I do understand, that I can't build an array of objects, since the objects are references theirself. But I don't know how to deal with that probelm.

Here is what i want to do in pseudo code:

int iLength_of_list = 3;
int list[3];
list[0] = 5;
list[1] = 7;
list[2] = 1;

StaticJsonBuffer<256> jsonBuffer;
JsonObject& joMessage = jsonBuffer.createObject();
joMessage["length of list"] = iLength_of_list;

/* some magic must happen here, similar to JsonObject& nestedObject[iLength_of_list] */

for (int i = 0; i<= iLength_of_list-1;i++) {
    char cBuffer[2];
    sprintf(cBuffer, "%d", i)
    nestedObject[i] = joMessage.createNestedObject(cBuffer);
    nestedObject[i]["en"] = englishTextOfIndex(list[i]);
    nestedObject[i]["de"] = germanTextOfIndex(list[i]);
}
joMessage.printTo(Serial)

I hope you can get an idea of what I am trying to do. I have read through all the issues and think that franklinvv in issue #10 had a similar problem, but the examplecode does not run for me.

Many thanks in advance

Chris

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions