json_object_array_add- Add json object to Array
Posted by Joys of Programming on in json-c
json_object_array is used to add a json object to an array
The syntax of the function is
int json_object_array_add (struct json_object *this, struct json_object *val)
Specify the object where the json object has to be added, key and the value (json object) as the arguments
The following program shows the usage of the function
#include <json/json.h>
#include <stdio.h>
int main() {
/*Creating a json object*/
json_object * jobj = json_object_new_object();
/*Creating a json array*/
json_object *jarray = json_object_new_array();
/*Creating json strings*/
json_object *jstring1 = json_object_new_string("c");
json_object *jstring2 = json_object_new_string("c++");
json_object *jstring3 = json_object_new_string("php");
/*Adding the above created json strings to the array*/
json_object_array_add(jarray,jstring1);
json_object_array_add(jarray,jstring2);
json_object_array_add(jarray,jstring3);
/*Form the json object*/
json_object_object_add(jobj,"Categories", jarray);
/*Now printing the json object*/
printf ("The json object created: %s\n",json_object_to_json_string(jobj));
}
The output of the program is something like this
The json object created: { "Categories": [ "c", "c++", "php" ] }
Comments: