Linux下cJSON编译及示例

1、下载源码解压编译安装 https://github.com/kbranigan/cJSON

make all

make PREFIX=/home/renzhong/cJSON-master install

PREFIX这个参数是设置安装路径,这里它只会安装动态库静态库在源码包的目录下能找到。经过这一步之后就会在安装路径下找到include和lib。

只需将cJSON.h cJSON.c 文件拷贝到所需使用的目录即可使用。

2、代码示例

#include 
#include 
#include 
#include "cJSON.h"

#define HU_MAX_SEND_LEN         512

int buffer_to_json1(void)
{
    char *data = "{\"love\":[\"LOL\",\"Go shopping\"]}";
    char *json_data = NULL;

    //从缓冲区中解析出JSON结构
    cJSON * json= cJSON_Parse(data);

    //将传入的JSON结构转化为字符串并打印
    json_data = cJSON_Print(json);
    printf("test:%lu:%s\n",strlen(data),data);
    printf("\ntest1:%lu\n%s\n",strlen(json_data),json_data);
    
    free(json_data);
    //将JSON结构所占用的数据空间释放
    cJSON_Delete(json);
    return 0;
}

int buffer_to_json2(void)
{
    char *string = "{\"family\":[\"father\",\"mother\",\"brother\",\"sister\",\"somebody\"]}";

    //从缓冲区中解析出JSON结构
    cJSON *json = cJSON_Parse(string);
    cJSON *node = NULL;
    cJSON *tnode = NULL;
    char *json_data = NULL;
    int size;
    int i;
    //cJOSN_GetObjectItem 根据key来查找json节点 若果有返回非空
    printf("test:%lu:%s\n",strlen(string),string);
    json_data = cJSON_Print(json);
    printf("\ntest2:%lu\n%s\n",strlen(json_data),json_data);

    node = cJSON_GetObjectItem(json,"family");
    if(node == NULL)
    {
        printf("family node == NULL\n");
    }
    else
    {
        printf("found family node\n");
    }
    node = cJSON_GetObjectItem(json,"fam");
    if(node == NULL)
    {
        printf("fam node == NULL\n");
    }
    else
    {
        printf("found fam node\n");
    }

    //判断是否有key是string的项 如果有返回1 否则返回0
    if(1 == cJSON_HasObjectItem(json,"family"))
    {
        printf("found family node\n");
    }
    else
    {
        printf("not found family node\n");
    }

    if(1 == cJSON_HasObjectItem(json,"fam"))
    {
        printf("found fam node\n");
    }
    else
    {
        printf("not found fam node\n");
    }
    
    node = cJSON_GetObjectItem(json,"family");
    if(node->type == cJSON_Array)
    {
        //非array类型的node 被当做array获取size的大小是未定义的行为 不要使用
        size = cJSON_GetArraySize(node);
        printf("array size is %d\n",size);
    }

    cJSON_ArrayForEach(tnode,node)
    {
        if(tnode->type == cJSON_String)
        {
            printf("int forEach: %s\n",tnode->valuestring);
        }
        else
        {
            printf("node's type is not string\n");
        }
    }
    
    return 0;
}

int creat_json1(void)
{
    //创建一个空的文档(对象)
    cJSON *array = NULL;
    char *buf = NULL;
    char data_temp[HU_MAX_SEND_LEN] = {0};
    char data_temp2[HU_MAX_SEND_LEN] = {0};
    cJSON *json = cJSON_CreateObject();
    int num = 0;
    
    //向文档中增加一个键值对{"name":"王大锤"}
    cJSON_AddItemToObject(json,"name",cJSON_CreateString("王大锤"));
    //cJSON_AddNumberToObject(json,"age",29);
    num = 28;
    cJSON_AddItemToObject(json,"age",cJSON_CreateNumber(num));
    
    cJSON_AddItemToObject(json,"love",array=cJSON_CreateArray());
    cJSON_AddItemToArray(array,cJSON_CreateString("LOL"));
    cJSON_AddItemToArray(array,cJSON_CreateString("NBA"));
    cJSON_AddItemToArray(array,cJSON_CreateString("Go shopping"));

    num = 58;
    cJSON_AddNumberToObject(json,"score",num);
    cJSON_AddStringToObject(json,"address","beijing");
    
    //将json结构格式化到缓冲区
    buf = cJSON_Print(json);
    printf("\ntest3:%lu\n%s\n",strlen(buf),buf);
    strcpy(data_temp,buf);
    buf = cJSON_PrintUnformatted(json);
    strcpy(data_temp2,buf);
    printf("\ntest333:%lu\n%s\n",strlen(buf),buf);

    free(buf);
    //释放json结构所占用的内存
    cJSON_Delete(json);

    //从缓冲区中解析出JSON结构
    json= cJSON_Parse(data_temp);
    buf = cJSON_Print(json);
    printf("\ntest11:%lu\n%s\n",strlen(buf),buf);
    free(buf);
    cJSON_Delete(json);

    json= cJSON_Parse(data_temp2);
    buf = cJSON_Print(json);
    printf("\ntest12:%lu\n%s\n",strlen(buf),buf);
    free(buf);
    cJSON_Delete(json);
    
    return 0;
}

int creat_json2(void)
{
    char *buf = NULL;
    cJSON *obj = NULL;
    char data_temp[HU_MAX_SEND_LEN] = {0};

    //先创建空对象
    cJSON *json = cJSON_CreateObject();
    //在对象上添加键值对
    cJSON_AddStringToObject(json,"country","china");
    //添加数组
    cJSON *array = NULL;
    cJSON_AddItemToObject(json,"stars",array=cJSON_CreateArray());
    //在数组上添加对象
    cJSON_AddItemToArray(array,obj=cJSON_CreateObject());
    //在对象上添加键值对
    cJSON_AddItemToObject(obj,"name",cJSON_CreateString("Faye"));
    cJSON_AddStringToObject(obj,"address","beijing");
    
    cJSON_AddItemToArray(array,obj=cJSON_CreateObject());
    cJSON_AddItemToObject(obj,"name",cJSON_CreateString("andy"));
    cJSON_AddItemToObject(obj,"address",cJSON_CreateString("HK"));
    
    cJSON_AddItemToArray(array,obj=cJSON_CreateObject());
    cJSON_AddStringToObject(obj,"name","eddie");
    cJSON_AddStringToObject(obj,"address","TaiWan");

    cJSON_AddBoolToObject(obj, "acc", 1);
    
    //将json结构格式化到缓冲区
    buf = cJSON_Print(json);
    printf("\ntest4:%lu\n%s\n",strlen(buf),buf);
    strcpy(data_temp,buf);

    //释放
    free(buf);
    cJSON_Delete(json);

    //解析
    
    return 0;
}

int main(void)
{
    /* print the version */
    printf("Version: %s\n", cJSON_Version());

    buffer_to_json1();
    buffer_to_json2();
    creat_json1();
    creat_json2();

    return 0;
}

 

你可能感兴趣的:(Linux下cJSON编译及示例)