The encapsulation and preservation of data key-value pairs using Cjson for Linux

Source: Internet
Author: User


First of all, we need to download the packaged Cjson package including Cjson.h CJSON.C

For the convenience of everyone I have uploaded

As follows

http://download.csdn.net/detail/wuheshi/8353075

Adding a header file when programming Cjson.h can call functions inside CJSON.C

Examples of packaging JSON data and parsing JSON data are:

Examples of simple encodings are as follows


#include ". /.. /include/myhead.h "
#include ". /.. /include/cjson.h "

/* Parse the JSON data out for useful information */
int Undo_cjson_pack (char text[],int id,int ctlcmd)
{
Cjson *json, *json_value;
Parsing packets
JSON = Cjson_parse (text);
if (!json)
{
printf ("Error before: [%s]\n", cjson_geterrorptr ()); Determine if it is JSON data
}
Else
{
Parse ID value
Json_value = Cjson_getobjectitem (JSON, "ID");
if (Json_value->type = = Cjson_number)
{
Get results from Valueint
printf ("value:%d\r\n", json_value->valueint);
id = json_value->valueint;
}
Analytic control Amount
Json_value = Cjson_getobjectitem (JSON, "ctl_cmd");
if (Json_value->type = = Cjson_number)
{
Get results in valuestring
printf ("cmd:%d\r\n", json_value->valueint);
Ctlcmd =json_value->valueint;
printf ("%s\n", cmd);
}
Free up memory space
Cjson_delete (JSON);
}
return 0;
}

/* Package Data Key value pairs into JSON format and pass to String */
char * do_cjson_pack (int idnum,char *str)
{
Create a JSON Object
Cjson *root = Cjson_createobject ();
Join node (key value pair), node name is ID, node value is 123.4
Cjson_addnumbertoobject (Root, "ID", idnum);
Cjson_addstringtoobject (Root, "ctl_cmd", str);
Print JSON packets
Char *out = cjson_print (root);
printf ("%s\n", out);
Freeing memory
Cjson_delete (root);
return out;
}


/*
int main ()
{
Char text[] = "{\" ctl_cmd\ ": \6,\" id\ ": 1}";
char *putout = NULL;
int cmd_str = 1;
int num = 9;

Putout = Do_cjson_pack (NUM,CMD_STR);
Undo_cjson_pack (text);

printf ("%s\n", putout);
Free (putout);
return 0;

}

*/


The program is a bit small change, the runtime error may be the data format a bit of a problem, here explain the role

The encapsulation and preservation of data key-value pairs using Cjson for Linux

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.