-
Notifications
You must be signed in to change notification settings - Fork 2
JSON_ADD_ARRAY_A
Jurek Muszyński edited this page Dec 19, 2021
·
2 revisions
Adds JSON array json_sub to JSON array.
None
// main record
JSON j={0};
JSON_ADD_STR(&j, "name", "John Smith");
JSON_ADD_INT(&j, "age", 35);
JSON_ADD_FLOAT(&j, "income", 12345.67);
JSON_ADD_BOOL(&j, "married", true);
// sub-record
JSON j_addr={0};
JSON_ADD_STR(&j_addr, "street", "10 Regent Street");
JSON_ADD_STR(&j_addr, "city", "London");
// add sub-record to main record
JSON_ADD_RECORD(&j, "address", &j_addr);
// array of records
JSON j_children={0};
// one array record
JSON j_child={0};
JSON_ADD_STR(&j_child, "name", "Anna");
JSON_ADD_INT(&j_child, "age", 2);
// add record to the array
JSON_ADD_RECORD_A(&j_children, 0, &j_child);
JSON_ADD_STR(&j_child, "name", "Paul");
JSON_ADD_INT(&j_child, "age", 4);
// add record to the array
JSON_ADD_RECORD_A(&j_children, 1, &j_child);
JSON_ADD_STR(&j_child, "name", "Sam");
JSON_ADD_INT(&j_child, "age", 6);
// add record to the array
JSON_ADD_RECORD_A(&j_children, 2, &j_child);
// add array to the main record
JSON_ADD_ARRAY(&j, "children", &j_children);
// stringify
json_string[NPP_JSON_BUFSIZE];
strcpy(json_string, JSON_TO_STRING_PRETTY(&j));
Result:
{
"name": "John Smith",
"age": 35,
"income": 12345.67,
"married": true,
"address":
{
"street": "10 Regent Street",
"city": "London"
},
"children":
[
{
"name": "Anna",
"age": 2
},
{
"name": "Paul",
"age": 4
},
{
"name": "Sam",
"age": 6
}
]
}