Create a nested json¶
Example on how to create a json object containing more json objects
Will create json:¶
{
"person": {
"name": "Hubert Cumberdale",
"age": 17
},
"vehicles": {
"bicycle": false,
"car": true,
"motorbike": false
}
}
Procedural (C++):¶
// Create empty JsonData of type JSON
Json json = JsonBlock();
// Create JsonData of type JSON to put in 'json'
Json person = JsonBlock();
person["name"] = "Hubert Cumberdale";
person["age"] = 17;
// Create JsonData of type JSON to put in 'json'
Json vehicles = JsonBlock();
vehicles["bicycle"] = false;
vehicles["car"] = true;
vehicles["motorbike"] = false;
// Now just toss them into 'json'
json["person"] = person;
json["vehicles"] = vehicles;
std::cout << json << std::endl;
Procedural, with shorthands (C++):¶
// Create empty JsonData of type JSON
Json json = JsonBlock();
json.AsJson.ShorthandAdd("person.name") = "Hubert Cumberdale";
json.AsJson.ShorthandAdd("person.age") = 17;
json.AsJson.ShorthandAdd("vehicles.bicycle") = false;
json.AsJson.ShorthandAdd("vehicles.car") = true;
json.AsJson.ShorthandAdd("vehicles.motorbike") = false;
std::cout << json << std::endl;
Add() is ONLY for adding!
Calling Add() with an already existing label/key will result in a JsonLabelAlreadyExistsException.
If you explicitly want to modify an existing value, use Get
("foobar").
SetStringData
("Never gonna up you give");
.
Do note that you can also call Set() which will kind of brute force it. Create the field if it doesn't exist, if it already does, modify it.
Same goes for "Shorthand" functions.
Set() is standard for the implicit syntax
If you do something like foo["bar"] = 5
, it is actually doing foo.Set("bar").SetIntData(5)
in the background.
So, you are actually calling Set().
Inline (C++):¶
Json json = JsonBlock({
Ele("person", JsonBlock({
Ele("name", "Hubert Cumberdale"),
Ele("age", 17)
})),
Ele("vehicles", JsonBlock({
Ele("bicycle", false),
Ele("car", true),
Ele("motorbike", false)
}))
});
std::cout << json << std::endl;