Read values from nested jsons¶
Example on how to read values from nested jsons.
We have this json:¶
{
"protagonist": {
"name": "Parry Hotter",
"age": 30,
"height": 184.9,
"isMale": false
},
"antagonist": {
"name": "Volkermord",
"evil_power": "no nose"
}
}
C++:¶
Json json;
json.Parse(aboveJsonCode);
std::string protago_name = json["protagonist"]["name"];
int protago_age = json["protagonist"]["age"];
float protago_height = json["protagonist"]["height"];
bool protago_isMale = json["protagonist"]["isMale"];
std::string antago_name = json["antagonist"]["name"];
std::string antago_power = json["antagonist"]["evil_power"];
This [""][""]
chain only works for getting existing values.
It will throw a JsonWrongDataTypeException if you try to chain-create fields like this.
Check out examples/implicit/creating/nested_json for a sleek way to do this.
With Shorthands (C++):¶
Json json;
json.Parse(aboveJsonCode);
std::string protago_name = json.AsJson.ShorthandGet("protagonist.name");
int protago_age = json.AsJson.ShorthandGet("protagonist.age");
float protago_height = json.AsJson.ShorthandGet("protagonist.height");
bool protago_isMale = json.AsJson.ShorthandGet("protagonist.isMale");
std::string antago_name = json.AsJson.ShorthandGet("antagonist.name");
std::string antago_power = json.AsJson.ShorthandGet("antagonist.evil_power");