Summary of Cjson Usage

Source: Internet
Author: User

Cjson is a handy lightweight JSON parser that can also be used to create JSON, written in C, with the latest code about 700 lines. Download Address

Https://github.com/DaveGamble/cJSON
https://sourceforge.net/projects/cjson/ Compilation Method (Cjson library, total two files: cjson.c, cJSON.h)

GCC cjson.c main.c-o main-lm Basic Interface Description

The JSON format description created: {} represents a object,[] representing an array

Cjson_createobject: Create a JSON structure body
Cjson_additemtoobject: Add an item to the JSON structure
Cjson_print: Convert JSON-formatted strings to normal strings
Cjson_parse: Converts a normal string into a JSON-formatted string
Cjson_delete: Releasing the JSON structure body

The following macro definitions can help you create JSON nodes more quickly

#define Cjson_addnulltoobject (Object,name)     cjson_additemtoobject (object, Name, Cjson_createnull ())
 # Define Cjson_addtruetoobject (Object,name)     cjson_additemtoobject (object, Name, Cjson_createtrue ())
 #define Cjson_addfalsetoobject (object,name)        cjson_additemtoobject (object, Name, Cjson_createfalse ())
 #define Cjson_addbooltoobject (object,name,b)   cjson_additemtoobject (object, name, Cjson_createbool (b))
 #define Cjson_addnumbertoobject (Object,name,n) Cjson_additemtoobject (object, name, Cjson_createnumber (n))
 #define Cjson_addstringtoobject (object,name,s) Cjson_additemtoobject (object, Name, cjson_createstring (s))

For example, if you want to add a "name": "Tom" node, the following two lines of code are equal

Cjson_addstringtoobject (Root, "name", "Tom")
Cjson_additemtoobject (Root, "name", Cjson_createstring ("Tom"))

Here's how JSON creates and parses the demo to create JSON demo1

    {
        ' name ': ' Tom ',
        ' age ': '
        marry ':        true, ' child
        ':        null
    }

Creating a Demo1 code implementation

char * cjson_create_test1 (void)
{
   char * out;
   Cjson *root;

   root = Cjson_createobject ();
   if (!root)
   {
       printf ("Cjson Create error!! \ n ");
       return NULL;
   }

   Cjson_additemtoobject (Root, "name", Cjson_createstring ("Tom"));
   Cjson_additemtoobject (Root, "age", cjson_createnumber);
   Cjson_additemtoobject (Root, "Marry", Cjson_createbool (1));
   Cjson_additemtoobject (Root, "child", Cjson_createnull ());

   Out=cjson_print (root);
   Cjson_delete (root);    

   if (out)
   {
       printf ('%s\n ', out);
       return out;
   }

   return NULL;
}
Demo2
{
    ' name ': ' Tom ',
    ' age ': '
    marry ':        true,
    ' children ':     [{
                    ' name '
                    ]: ' May ', ' Age ':  2
            }, {
                    ' name ': ' Kendy ',
                    ' age ':  3
            }, {
                    ' name ': ' Mark ',
                    ' age ':  1
            }]
}

Creating a Demo2 implementation method

char * cjson_create_test2 (void) {char * out;
    Cjson *root;

    Cjson *child, *child1, *child2, *child3;
    root = Cjson_createobject (); if (!root) {printf ("Cjson Create error!!
        \ n ");
    return NULL;
    Cjson_additemtoobject (Root, "name", Cjson_createstring ("Tom"));
    Cjson_additemtoobject (Root, "age", Cjson_createnumber (25));

    Cjson_additemtoobject (Root, "Marry", Cjson_createbool (1));    
    Child = Cjson_createarray ();
    Cjson_chk_fail (Child, NULL);

    Cjson_additemtoobject (Root, "children", child);    
    Child1 = Cjson_createobject ();
    Cjson_chk_fail (Child1, NULL);
    Cjson_additemtoobject (child1, "name", Cjson_createstring ("may"));

    Cjson_additemtoobject (Child1, "Age", Cjson_createnumber (2));    
    Child2 = Cjson_createobject ();
    Cjson_chk_fail (Child2, NULL);
    Cjson_additemtoobject (child2, "name", Cjson_createstring ("Kendy"));

    Cjson_additemtoobject (Child2, "Age", Cjson_createnumber (3)); CHILD3 = Cjson_createObject ();
    Cjson_chk_fail (child3, NULL);
    Cjson_additemtoobject (child3, "name", Cjson_createstring ("Mark"));

    Cjson_additemtoobject (child3, "Age", Cjson_createnumber (1));
    Cjson_additemtoarray (Child, child1);
    Cjson_additemtoarray (Child, child2);

    Cjson_additemtoarray (Child, child3);
    Out=cjson_print (root);    

    Cjson_delete (root);      
        if (out) {printf ('%s\n ', out);
    return out;
return NULL; }
ways to parse JSON parse the demo1 above
void Cjson_parse_test1 (void)
{
    Cjson * root;
    Cjson *name, *age, *marry, *child;

    Root = Cjson_parse (Cjson_create_test1 ());   
    if (!root)
    {
        printf ("Cjson_parse error!! \ n ");
        return;
    }

    Name = Cjson_getobjectitem (Root, "name");
    if (name)
    {
        printf ("Get Name Value:%s\n", name->valuestring);
    }

    Age = Cjson_getobjectitem (Root, "age");
    if (age)
    {
        printf (' Get Age Value:%d\n ', age->valueint);
    }

    Marry = Cjson_getobjectitem (root, "marry");
    if (marry)
    {
        printf ("Get Marry Value:%d\n", marry->valueint);
    }

    Child = Cjson_getobjectitem (Root, "child");
    if (child)
    {
        printf (' Get Child value:%s\n ', marry->valuestring);
    }

    Cjson_delete (root);    
}
parse the Demo2 above
void Cjson_parse_test2 (void) {Cjson * root;
    Cjson *name, *age, *marry, *children;

    Cjson *childnode, *childname, *childage;   
    Root = Cjson_parse (Cjson_create_test2 ()); if (!root) {printf ("Cjson_parse error!!
        \ n ");
    Return
    Name = Cjson_getobjectitem (Root, "name");
    if (name) {printf ("Get Name Value:%s\n", name->valuestring);
    Age = Cjson_getobjectitem (root, ' age ');
    if (age) {printf (' Get Age Value:%d\n ', age->valueint);
    } marry = Cjson_getobjectitem (root, "marry");
    if (marry) {printf ("Get Marry Value:%d\n", marry->valueint);
    } children = Cjson_getobjectitem (root, "children");
        if (children) {int size = Cjson_getarraysize (children);

        printf ("Get Children Size:%d\n", size);
        int i = 0;
            For (i=0 i<size; i++) {Childnode = Cjson_getarrayitem (children, I);
         if (Childnode)   {childname = Cjson_getobjectitem (Childnode, "name"); if (childname) {printf ("Get Child Name:%s\n", childname->valuestring)
                ;
                } childage = Cjson_getobjectitem (Childnode, "age");
                if (childage) {printf ("Get Child Age:%d\n", childage->valueint);    
}}} cjson_delete (root); }

PS: Here is a convenient online JSON format checksum and formatted Web site
http://pro.jsonlint.com/

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.