Skip to content

Create a multidimensional array


Example on how to create a multi-dimensional array.
The main purpose of this is to show you that you can frankenstein all components together as you like.
For demonstration purposes, we'll represent a 3x3x3 cubic cartesian coordinate system.

Will create json:
{
    "cube_values": [
        [
            [ 0, 0, 0 ],
            [ 0, 0, 0 ],
            [ 0, 0, 0 ]
        ],
        [
            [ 0, 0, 0 ],
            [ 0, 0, 0 ],
            [ 0, 0, 0 ]
        ],
        [
            [ 0, 0, 0 ],
            [ 0, 0, 0 ],
            [ 0, 0, 0 ]
        ]
    ]
}
Procedural (C++):
Json json = JsonData(JsonBlock()); // Create a JsonBlock to house everything

// Let's actually create it by iterating through a cartesian coordinate system!

JsonArray arrX;
for (unsigned int x = 0; x < 3; x++)
{
    JsonArray arrY;
    for (unsigned int y = 0; y < 3; y++)
    {
        JsonArray arrZ;
        for (unsigned int z = 0; z < 3; z++)
        {
            // Some value bound to these coordinates.
            // Hell, if you wanted, you could even put another JsonBlock in there.
            arrZ.Add(0);
        }
        arrY.Add(arrZ);
    }
    arrX.Add(arrY);
}

json.AsJson.Add("cube_values").SetArrayData(arrX);

std::cout << json.Render() << std::endl;
Inline (C++):
Json json = JsonData(JsonBlock({
    Ele("cube_values", JsonArray({
        JsonData(JsonArray({
            JsonData(JsonArray({0, 0, 0})),
            JsonData(JsonArray({0, 0, 0})),
            JsonData(JsonArray({0, 0, 0}))
        })),
        JsonData(JsonArray({
            JsonData(JsonArray({0, 0, 0})),
            JsonData(JsonArray({0, 0, 0})),
            JsonData(JsonArray({0, 0, 0}))
        })),
        JsonData(JsonArray({
            JsonData(JsonArray({0, 0, 0})),
            JsonData(JsonArray({0, 0, 0})),
            JsonData(JsonArray({0, 0, 0}))
        }))
    }))
}));

std::cout << json.Render() << std::endl;