Cjson 函数说明与使用
cJSON
Ultralightweight JSON parser in ANSI C
项目地址:https://gitcode.com/gh_mirrors/cj/cJSON
免费下载资源
·
了解Cjson的函数使用说明,首先需要获取Cjson的源文件
https://sourceforge.net/projects/cjson/
相应的函数进行说明
/* Supply a block of JSON, and this returns a cJSON object you can interrogate. Call cJSON_Delete when finished. */
extern cJSON *cJSON_Parse(const char *value);//从 给定的json字符串中得到cjson对象
/* Render a cJSON entity to text for transfer/storage. Free the char* when finished. */
extern char *cJSON_Print(cJSON *item);//从cjson对象中获取有格式的json对象
/* Render a cJSON entity to text for transfer/storage without any formatting. Free the char* when finished. */
extern char *cJSON_PrintUnformatted(cJSON *item);//从cjson对象中获取无格式的json对象
/* Delete a cJSON entity and all subentities. */
extern void cJSON_Delete(cJSON *c);//删除cjson对象,释放链表占用的内存空间
/* Returns the number of items in an array (or object). */
extern int cJSON_GetArraySize(cJSON *array);//获取cjson对象数组成员的个数
/* Retrieve item number "item" from array "array". Returns NULL if unsuccessful. */
extern cJSON *cJSON_GetArrayItem(cJSON *array,int item);//根据下标获取cjosn对象数组中的对象
/* Get item "string" from object. Case insensitive. */
extern cJSON *cJSON_GetObjectItem(cJSON *object,const char *string);//根据键获取对应的值(cjson对象)
/* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to look a few chars back to make sense of it. Defined when cJSON_Parse() returns 0. 0 when cJSON_Parse() succeeds. */
extern const char *cJSON_GetErrorPtr(void);//获取错误字符串
调用相应的函数进行演示
char * makeJson()
{
cJSON * pJsonRoot = NULL;
pJsonRoot = cJSON_CreateObject();
if(NULL == pJsonRoot)
{
printf("malloc error 1");
return NULL;
}
cJSON_AddStringToObject(pJsonRoot, "name", "xiaohu");
cJSON_AddNumberToObject(pJsonRoot, "id", 1001001);
cJSON_AddBoolToObject(pJsonRoot, "bool", 1);
cJSON * pSubJson = NULL;
pSubJson = cJSON_CreateObject();
if(NULL == pSubJson)
{
printf("malloc error 2");
cJSON_Delete(pJsonRoot);
return NULL;
}
cJSON_AddStringToObject(pSubJson, "dataobj", "a data about someperson");
cJSON_AddItemToObject(pJsonRoot, "data", pSubJson);
char * p = cJSON_Print(pJsonRoot);
if(NULL == p)
{
printf("malloc error 3");
cJSON_Delete(pJsonRoot);
return NULL;
}
//free(p);
cJSON_Delete(pJsonRoot);
return p;
}
void parseJson(char * pMsg)
{
if(NULL == pMsg)
{
printf("malloc error 4");
return;
}
cJSON * pJson = cJSON_Parse(pMsg);
if(NULL == pJson)
{
printf("malloc error 5");
return ;
}
// get string from json
cJSON * pSub = cJSON_GetObjectItem(pJson, "name");
if(NULL == pSub)
{
printf("malloc error 6");
}
printf("obj_1 : %s\n", pSub->valuestring);
// get number from json
pSub = cJSON_GetObjectItem(pJson, "id");
if(NULL == pSub)
{
printf("malloc error 7");
}
printf("obj_2 : %d\n", pSub->valueint);
// get bool from json
pSub = cJSON_GetObjectItem(pJson, "bool");
if(NULL == pSub)
{
printf("malloc error 8");
}
printf("obj_3 : %d\n", pSub->valueint);
// get sub object
pSub = cJSON_GetObjectItem(pJson, "data");
if(NULL == pSub)
{
printf("malloc error 9");
}
cJSON * pSubSub = cJSON_GetObjectItem(pSub, "dataobj");
if(NULL == pSubSub)
{
printf("malloc error 10");
}
printf("sub_obj_1 : %s\n", pSubSub->valuestring);
cJSON_Delete(pJson);
}
int main()
{
char * p = makeJson();
if(NULL == p)
{
return 0;
}
printf("%s\n", p);
parseJson(p);
free(p);
return 1;
}
相应的结果如下
GitHub 加速计划 / cj / cJSON
10.45 K
3.16 K
下载
Ultralightweight JSON parser in ANSI C
最近提交(Master分支:3 个月前 )
424ce4ce
This reverts commit 5b502cdbfb21fbe5f6cf9ffbd2b96e4281a741e6.
Related to #860
4 个月前
32497300 - 6 个月前
更多推荐
已为社区贡献1条内容
所有评论(0)