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 = 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 place of the 0.
            arrZ += 0;
        }
        arrY += arrZ;
    }
    arrX += arrY;
}

json["cube_values"] = arrX;

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

std::cout << json << std::endl;