里面包含两个文件. 一个是.h的头文件,一个是c的源程序 使用前需要先执行 gcc -c cJSON.c 生成cJSON.o文件 然后在你的程序中进行包含 #include "cJSON.h" 编译时 需要跟随参数 gcc空格-o空格[要生成的文件名]空格[你的源文件.c]空格cJSON.o空格`mysql_config空格--cflags空格--libs`空格-D_GNU_SOURCE空格-D__USE_XOPEN 例如: gcc -o test test.c cJSON.o `mysql_config --cflags --libs` -D_GNU_SOURCE -D__USE_XOPEN 笔者用的gcc是架构在centos 系统上的, 至于其他系统是否也有同样的要求就不是很确认了 另外 附赠 操作cJSON 可能需要前置包 一下是我用到的 #include <errno.h> #include <stdio.h> #include <time.h> #include <stdlib.h> #include <string.h> #include <arpa/inet.h> #include <sys/socket.h> #include <mysql/mysql.h> #include <unistd.h> #include <sys/shm.h> #include "cJSON.h" 下面是操作实例 数组转换为JSON cJSON *YearMonthJson = cJSON_CreateObject(); cJSON_AddStringToObject(YearMonthJson,"UpReferKey", Belong2Partner.UpReferKey ); cJSON_AddNumberToObject(YearMonthJson,"All", Belong2Partner.All ); cJSON *EachDayJson = cJSON_CreateArray(); cJSON_AddItemToObject(YearMonthJson,"EachDay",EachDayJson); for(int i=0;i<31;i++){ cJSON *EachDayJsonChild = cJSON_CreateArray(); cJSON_AddItemToArray(EachDayJson,EachDayJsonChild); for(int j=0;j<4; j++){ cJSON_AddItemToArray(EachDayJsonChild, cJSON_CreateNumber(Belong2PartnerEachDay[j])); } } 输出 printf("【%d】ChangeLogJson = '%s'\n",__LINE__,cJSON_PrintUnformatted(YearMonthJson)); 下面是json转换为数组 cJSON *YearMonthJson = cJSON_CreateObject(); YearMonthJson = cJSON_Parse(PartnerRow); if(cJSON_GetObjectItem(YearMonthJson,"All")) Belong2Partner->All = cJSON_GetObjectItem(YearMonthJson,"All")->valuelong; else Belong2Partner->All =0; cJSON *EachDayJson=cJSON_CreateArray(); if(cJSON_GetObjectItem(YearMonthJson,"EachDay")){ EachDayJson= cJSON_GetObjectItem(YearMonthJson,"EachDay"); } for(int i=0;i<31;i++){ cJSON *EachDayJsonChild = cJSON_CreateObject(); if(cJSON_GetArrayItem(EachDayJson,i)){ 数组[i]=cJSON_GetArrayItem(EachDayJson,i); } }
- 1
- 粉丝: 1
- 资源: 9
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助