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