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;
}

相应的结果如下

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值