Starting with a json string borrowed from https://json.org/example.html
Code: Select all
string json_string_data = "{\"widget\": {\"debug\": \"on\", \"window\": {\"title\": \"Sample Konfabulator Widget\", \"name\": \"main_window\", \"width\": 500, \"height\": 500 }, \"image\": {\"src\": \"Images/Sun.png\", \"name\": \"sun1\", \"hOffset\": 250, \"vOffset\": 250, \"alignment\": \"center\"}, \"text\": {\"data\": \"Click Here\", \"size\": 36, \"style\": \"bold\", \"name\": \"text1\", \"hOffset\": 250, \"vOffset\": 100, \"alignment\": \"center\", \"onMouseUp\": \"sun1.opacity = (sun1.opacity / 100) * 90;\"} }}";
Code: Select all
jsonnode root = json_parse(json_string_data);
Here we will grab the text size.
Code: Select all
float f_text_size = json_get_float(root.widget.text.size);
Code: Select all
string nodeName = json_get_name(root.widget.text.size);
over the length, with a child index like so:
Code: Select all
for (int idx = 0; idx < json_get_length(root); idx++)
{
jsonnode node = json_get_child_at_index(idx);
string nodeName = json_get_name(node);
}
If we are parsing json data that we have no previous knowledge of, we will need to query for the data types so we know which json_get_<type>() function to call.
Code: Select all
float data_type = json_get_value_type(node);
Code: Select all
enum json_type_e : int
{
JSON_TYPE_STRING,
JSON_TYPE_NUMBER,
JSON_TYPE_OBJECT,
JSON_TYPE_ARRAY,
JSON_TYPE_TRUE,
JSON_TYPE_FALSE,
JSON_TYPE_NULL
};
Code: Select all
json_free(root);