JSON Section API Description:
Sample program:
#include <stdio.h> #include <stdlib.h> #include <stddef.h> #include <string.h> #include "json.h "Int main (int argc, char **argv) {struct Json_tokener *tok; struct Json_object *my_string, *my_int, *my_object, *my_array; struct Json_object *new_obj; int i; my_string = json_object_new_string ("\ t");/* Output my_string= */printf ("my_string=%s\n", Json_object_get_string (My_ string)); \t/* convert JSON format string output my_string.to_string () = "\ T" */printf ("my_string.to_string () =%s\n", Json_object_to_json_string ( my_string)); "\ T"/* Release resources */Json_object_put (my_string); my_string = json_object_new_string ("\ \"); printf ("my_string=%s\n", Json_object_get_string (my_string)); printf ("my_string.to_string () =%s\n", Json_object_to_json_string (my_string)); \ json_object_put (my_string); my_string = json_object_new_string ("foo"); printf ("my_string=%s\n", Json_object_get_string (my_string)); printf ("my_string.to_string () =%s\n", Json_object_to_json_string (my_string)); Foo My_int = JSON_object_new_int (9); printf ("my_int=%d\n", Json_object_get_int (My_int)); printf ("my_int.to_string () =%s\n", Json_object_to_json_string (My_int)); 9/* Create an empty JSON object value array type */My_array = Json_object_new_array (); /* Add JSON value type to array */Json_object_array_add (My_array, Json_object_new_int (1)); Json_object_array_add (My_array, Json_object_new_int (2)); Json_object_array_add (My_array, Json_object_new_int (3)); Json_object_array_put_idx (My_array, 4, Json_object_new_int (5)); printf ("my_array=\n"); for (i=0; i < json_object_array_length (My_array); i++) {struct Json_object *obj = Json_object_array_get_idx (my_array , i); printf ("\t[%d]=%s\n", I, json_object_to_json_string (obj)); } printf ("My_array.to_string () =%s\n", Json_object_to_json_string (My_array)); 1 2 3 null 5 My_object = Json_object_new_object (); /* Add JSON name and value to the JSON object collection */Json_object_object_add (my_object, "abc", Json_object_new_int (12)); Json_object_object_add (My_object, "foo", Json_object_new_string ("Bar")); Json_object_oBject_add (My_object, "Bool0", Json_object_new_boolean (0)); Json_object_object_add (My_object, "Bool1", Json_object_new_boolean (1)); Json_object_object_add (My_object, "Baz", Json_object_new_string ("bang")); /* The same key addition will replace */Json_object_object_add (my_object, "Baz", Json_object_new_string ("Fark")); Json_object_object_del (My_object, "Baz"); printf ("my_object=\n"); /* Iterate through the collection of JSON objects */Json_object_object_foreach (My_object, Key, Val) {printf ("\t%s:%s\n", Key, Json_object_to_json_string ( val)); } printf ("My_object.to_string () =%s\n", Json_object_to_json_string (My_object)); /* Some of the irregular strings do some analytical tests */new_obj = Json_tokener_parse ("\" \003\ ""); printf ("new_obj.to_string () =%s\n", Json_object_to_json_string (New_obj)); Json_object_put (New_obj); New_obj = Json_tokener_parse ("/* Hello */\" foo\ ""); printf ("new_obj.to_string () =%s\n", Json_object_to_json_string (New_obj)); Json_object_put (New_obj); New_obj = Json_tokener_parse ("//hello\n\" foo\ ""); printf ("new_obj.to_string () =%s\n", json_object_To_json_string (new_obj)); Json_object_put (New_obj); New_obj = Json_tokener_parse ("\" \\u0041\\u0042\\u0043\ ""); printf ("new_obj.to_string () =%s\n", Json_object_to_json_string (New_obj)); Json_object_put (New_obj); New_obj = Json_tokener_parse ("null"); printf ("new_obj.to_string () =%s\n", Json_object_to_json_string (New_obj)); Json_object_put (New_obj); New_obj = Json_tokener_parse ("True"); printf ("new_obj.to_string () =%s\n", Json_object_to_json_string (New_obj)); Json_object_put (New_obj); New_obj = Json_tokener_parse ("12"); printf ("new_obj.to_string () =%s\n", Json_object_to_json_string (New_obj)); Json_object_put (New_obj); New_obj = Json_tokener_parse ("12.3"); /* Get JSON Double type printf ("new_obj.to_string () =%s\n", Json_object_to_json_string (New_obj)); Json_object_put (New_obj); New_obj = Json_tokener_parse ("[\" \\n\ "]"); printf ("new_obj.to_string () =%s\n", Json_object_to_json_string (New_obj)); Json_object_put (New_obj); New_obj = Json_tokener_parse ("[\" \\nabc\\n\ "]"); printf ("New_obj.to_string () =%s\n ", Json_object_to_json_string (New_obj)); Json_object_put (New_obj); New_obj = Json_tokener_parse ("[null]"); printf ("new_obj.to_string () =%s\n", Json_object_to_json_string (New_obj)); Json_object_put (New_obj); New_obj = Json_tokener_parse ("[]"); printf ("new_obj.to_string () =%s\n", Json_object_to_json_string (New_obj)); Json_object_put (New_obj); New_obj = Json_tokener_parse ("[false]"); printf ("new_obj.to_string () =%s\n", Json_object_to_json_string (New_obj)); Json_object_put (New_obj); New_obj = Json_tokener_parse ("[\" Abc\ ", null,\" def\ ", 12]"); printf ("new_obj.to_string () =%s\n", Json_object_to_json_string (New_obj)); Json_object_put (New_obj); New_obj = Json_tokener_parse ("{}"); printf ("new_obj.to_string () =%s\n", Json_object_to_json_string (New_obj)); Json_object_put (New_obj); New_obj = Json_tokener_parse ("{\" foo\ ": \" Bar\ "}"); printf ("new_obj.to_string () =%s\n", Json_object_to_json_string (New_obj)); Json_object_put (New_obj); New_obj = Json_tokener_parse ("{\ "foo\": \ "Bar\", \ "baz\": null, \ "Bool0\": true} "); printf ("new_obj.to_string () =%s\n", Json_object_to_json_string (New_obj)); Json_object_put (New_obj); New_obj = Json_tokener_parse ("{\" foo\ ": [null, \" foo\ "]}"); printf ("new_obj.to_string () =%s\n", Json_object_to_json_string (New_obj)); Json_object_put (New_obj); New_obj = Json_tokener_parse ("{\" abc\ ":" \ "foo\": \ "Bar\", \ "Bool0\": false, \ "Bool1\": true, \ "Arr\": [1, 2, 3, NULL , 5]} "); printf ("new_obj.to_string () =%s\n", Json_object_to_json_string (New_obj)); Json_object_put (New_obj); New_obj = Json_tokener_parse ("{foo}"); if (Is_error (new_obj)) printf ("Got error as expected\n"); New_obj = Json_tokener_parse ("foo"); if (Is_error (new_obj)) printf ("Got error as expected\n"); New_obj = Json_tokener_parse ("{\" foo "); if (Is_error (new_obj)) printf ("Got error as expected\n"); /* Test Incremental parsing */tok = json_tokener_new (); New_obj = JSON_TOKENER_PARSE_EX (tok, "{\" foo ", 6); if (Is_error (new_obj)) printf ("Got errOr as expected\n "); New_obj = JSON_TOKENER_PARSE_EX (tok, "\": {\ "bar", 8); if (Is_error (new_obj)) printf ("Got error as expected\n"); New_obj = JSON_TOKENER_PARSE_EX (tok, "\": 13} ", 6); printf ("new_obj.to_string () =%s\n", Json_object_to_json_string (New_obj)); Json_object_put (New_obj); Json_tokener_free (Tok); Json_object_put (my_string); Json_object_put (My_int); Json_object_put (My_object); Json_object_put (My_array); /* If the object is not previously added to the object, it must be displayed, if it is added to the object, it is not required to be called, it is important to be careful, otherwise it is easy to memory leak */return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
JSON API descriptions and sample programs