Cjson Study notes sequel

Source: Internet
Author: User

0. PrefaceThis article attempts to illustrate how to construct a variety of JSON packets using Cjson. I have written a Cjson article in the previous period, so this article becomes "sequel".
"Related blog posts""Learning--json at the front"-learning a variety of JSON formats
"Cjson Study Notes"--the previous "set" of this study note.
"Code Warehouse"--cjson-example Code Warehouse is located in BitBucket using HG (instead of Git), HGin Windows or Ubuntu have a very good GUI tools--tortoisehg, I am stupid to master git. "TORTOISEHG Instructions"-if you have not used HG please refer to the blog, refer to the Clone section operation.

"Test Tool""Ubuntu" ECLIPSE+GCC "Windows" Eclipse+mingw
1. Important Function Description"1" two create"Create JSON Object" Cjson *cjson_createobject (void);
"Create JSON array" Cjson *cjson_createarray (void);

"2" Two kinds of add"Add to Object" voidCjson_additemtoobject (Cjson *object,const char *string,cjson *item);
"Add to Array" void Cjson_additemtoarray (Cjson *array, Cjson *item);

"3" a few common strokes"Add number to Object" Cjson_additemtoobject (Root, "value", Cjson_createnumber (value));
"Add File to Object" Cjson_additemtoobject (Root, "string", Cjson_createstring (string));

"4" JSON nesting"Add array to Object" Cjson_additemtoobject (Root, "rows", rows = Cjson_createarray ());
"Add object to Array" Cjson_additemtoarray (rows, row = Cjson_createobject ());

2. Create a variety of JSON packetsHere are a few examples of the code, please see the relevant files in the Code warehouse for more information.
"1" JSON number
void Create_single_number (void) {    Cjson *root;    char *out;    int value =;    root = Cjson_createobject (); Create Root    cjson_additemtoobject (root, "value", Cjson_createnumber (value));    Print and release out    = Cjson_print (root); Cjson_delete (root); printf ("%s\n", out)    ; Console output # if 0    {        "value":    #endif}
"Simple description""1" cjson_additemtoobject (Root, "value", Cjson_createnumber (value));     "2" cjson_addnumbertoobject (Root, "value", value); The "1" and "2" effects are exactly the same.
"2" JSON string
void create_single_string (void) {    Cjson *root;    char *out;    Char *name = "xukai871105";    root = Cjson_createobject (); To create a root    //method using Cjson_additemtoobject, it is recommended to use    cjson_additemtoobject (root, "name", cjson_createstring (name));    Print and release out    = Cjson_print (root); Cjson_delete (root); printf ("%s\n", out)    ; Console output # if 0    {        "name": "xukai871105"    } #endif}
"Simple description""1" cjson_additemtoobject (Root, "name", cjson_createstring (name));    "2" cjson_addstringtoobject (Root, "name", name); The "1" and "2" effects are exactly the same.
"3" JSON Boolean type
void Create_bool (void) {    Cjson *root;    char *out;    root = Cjson_createobject (); Create Root    cjson_additemtoobject (root, "Success", Cjson_createfalse ());    Print and release out    = Cjson_print (root); Cjson_delete (root); printf ("%s\n", out)    ; Console output # if 0    {        "success": false    } #endif}
"Simple description"The "1" Boolean type does not need to be quoted.
Nested JSON formats in 3.JSON format tend to be nested when used, such as JSON objects nested in JSON arrays and JSON arrays in JSON objects, following a few simple examples to illustrate the problem.
"1" json simple nesting
void Create_simple (void) {    Cjson *root;    char *out;    int list[4] = {5,6,7,8};    root = Cjson_createobject (); Create Root    cjson_additemtoobject (root, "lists", Cjson_createintarray (list, 4));    Print and release out    = Cjson_print (root); Cjson_delete (root); printf ("%s\n", out)    ; Console output # if 0    {        "lists": [5, 6, 7, 8]    } #endif}
    "2" JSON complex nested
void Create_complex (void) {Cjson *root, *rows, *row;char *out;int i = 0;char *title[3] = {"Raspberry Pi study notes--index post", "Raspberry Pi study notes--gpio Gong" Can learn "," Internet of Things Learning notes-index POST "};char *url[3] = {" http://blog.csdn.net/xukai871105/article/details/23115627 "," http:// blog.csdn.net/xukai871105/article/details/12684617 "," http://blog.csdn.net/xukai871105/article/details/23366187 "};root = Cjson_createobject (); Create root cjson_addnumbertoobject (root, "Total", 3);//Add Arraycjson_additemtoobject to Object (root, "rows", rows = Cjson_ Createarray ()); for (i = 0; i < 3; i++) {//Add Objectcjson_additemtoarray to Array (rows, row = Cjson_createobject ()); Cjson_ Additemtoobject (Row, "title", Cjson_createstring (Title[i])), Cjson_additemtoobject (row, "url", cjson_createstring ( Url[i]));} Print and release out = Cjson_print (root); Cjson_delete (root); printf ("%s\n", out); Free;//console Output # If 0{"Total": 3, "Rows": [{"title": "Raspberry Pi Learning Note-index post", "url": "http://blog.csdn.net/xukai871105/ article/details/23115627 "}, {" title ":" Raspberry Pi Learning note--gpio functional learning "," url ":" Http://blog.csdn.net/xukai871105/article/details/12684617 "}, {" title ":" IoT learning note-index post "," url ":" http://blog.csdn.net/xukai871105/ article/details/23366187 "}]} #endif}
"Simple description"Rows is a JSON object, and a JSON array is nested in the Rows object, and the element of each JSON array is a JSON object that has two key-value pairs in the Row,row object, namely Titile and URLs.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.