【cJSON】cJSON 实例

 C++ Code 

主要函数:

    【1】常用创建
    【创建JSON对象】cJSON *cJSON_CreateObject( void );
    【创建JSON数组】cJSON *cJSON_CreateArray(
void );
    
    【2】常用添加
    【向对象中添加对象】voidcJSON_AddItemToObject(cJSON *object,
const   char  *string,cJSON *item);
    【向数组中添加对象】void cJSON_AddItemToArray(cJSON *array, cJSON *item);
    【向对象中添加数值】cJSON_AddNumberToObject(object,name,n)
            【向对象中添加字符串】 cJSON_AddStringToObject(object,name,s)

示例:
#include
#include   "cJSON.h"

char  *makeJson()
{
    cJSON *pJsonRoot  = 
NULL ;
    cJSON *pIntArray  = 
NULL ;
    cJSON *pCommArray = 
NULL ;
    cJSON *pSubJson   = 
NULL ;
    
char    *p          =  NULL ;
    
int  intarr[ 5 ] = { 0 1 2 3 4 };  //整数数组


    pJsonRoot = cJSON_CreateObject(); 
//创建JSON对象

    
if ( NULL  == pJsonRoot)
    {
        
//error happend here
         return   NULL ;
    }

    cJSON_AddStringToObject(pJsonRoot, 
"hello" "hello world" );  //添加一个值为字符串的键值对"hello":"hello world"
    cJSON_AddNumberToObject(pJsonRoot,  "number" 10010 );         //添加一个值为数值的键值对"number": 10010
    cJSON_AddBoolToObject(pJsonRoot,  "bool" 1 );                 //添加一个值为布尔的键值对"bool": 1



    pSubJson = cJSON_CreateObject();     
//创建另一个json对象作为子对象

    
if ( NULL  == pSubJson)
    {
        
// create object faild, exit
        cJSON_Delete(pJsonRoot);          //删除pJsonRoot 及其子对象
         return   NULL ;
    }

    cJSON_AddStringToObject(pSubJson, 
"subjsonobj" "a sub json string" );  //添加一个值为布尔的键值对
    cJSON_AddItemToObject(pJsonRoot,  "subobj" , pSubJson);                    //将对象pSubJson添加到pJsonRoot中,成为键值对 "subobj":pSubJson


    
//数值数组
    pIntArray = cJSON_CreateIntArray(intarr,  5 );  //为intarr创建一个数值数组对象,
    cJSON_AddItemToObject(pJsonRoot,  "IntArr" , pIntArray);   //将对象pIntArray添加到pJsonRoot中,成为键值对 " IntArr ":pIntArray


    
//通用数组

    pCommArray = cJSON_CreateArray();
//创建数组对象
     //cJSON_AddItemToArray(cJSON *array, cJSON *item);//向数组中添加对象
    cJSON_AddNumberToObject(pCommArray,  "num" 2 );              //向数组中添加数值cJSON_AddNumberToObject(object,name,n)
    cJSON_AddStringToObject(pCommArray,  "str" "hopeview" );     //向对象中添加字符串 cJSON_AddStringToObject(object,name,s)


    cJSON_AddItemToObject(pJsonRoot, 
"pCommArray " , pCommArray);   //将对象pCommArray添加到pJsonRoot中,成为键值对 " pCommArray ":pCommArray


    p = cJSON_Print(pJsonRoot);   
//json对象转为json字符串用于传输

    
// else use :
     // char * p = cJSON_PrintUnformatted(pJsonRoot);
     if ( NULL  == p)
    {
        
//convert json list to string faild, exit
         //because sub json pSubJson han been add to pJsonRoot, so just delete pJsonRoot, if you also delete pSubJson, it will coredump, and error is : double free
        cJSON_Delete(pJsonRoot);  //删除pJsonRoot 及其子对象
         return   NULL ;
    }

    
//free(p);

    cJSON_Delete(pJsonRoot); 
//释放json对象
    printf( "myJson is:%s" ,p);
    
return  p;                //返回json字符串,注意外面用完p要记得释放空间。//free(p);
}

posted on   bdy  阅读(266)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示