Re: how to use <insert here> in CSQC
Posted: Mon May 08, 2023 4:23 am
Json tutorial continued.
Starting with a json string borrowed from https://json.org/example.html
We parse the string into a json node.
Next we probably want the data, we grab that using the apropriate json_get_<type> method.
Here we will grab the text size.
If instead of the data, we had just wanted to know what that data was called, we would grab its name
If we wanted to iterate over the top level objects of the node, we could use a for loop
over the length, with a child index like so:
We loop over array nodes the same way.
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.
Calling that function will result in one of the values from this enum:
Lastly, when we are done with our jsonnode, we should free it back up with a call to:
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);