2015/08/19 17:50
【其他参考示例】
宏的定义原型:
/* Macros for creating things quickly. */
#define cJSON_AddNullToObject(object,name) cJSON_AddItemToObject(object,name,cJSON_CreateNull())
#define cJSON_AddTrueToObject(object,cJSON_CreateTrue())
#define cJSON_AddFalseToObject(object,cJSON_CreateFalse())
#define cJSON_AddBoolToObject(object,b) cJSON_AddItemToObject(object,cJSON_CreateBool(b))
#define cJSON_AddNumberToObject(object,n) cJSON_AddItemToObject(object,cJSON_CreateNumber(n))
#define cJSON_AddStringToObject(object,s) cJSON_AddItemToObject(object,cJSON_CreateString(s))
示例:
cJSON *root = cJSON_CreateObject();
//先创建一个对象
cJSON_AddNumberToObject(root,123.4);
cJSON_AddStringToObject(root,"year","2015");
cJSON_AddNullToObject(root,"secret");
cJSON_AddTrueToObject(root,"Bool");
cJSON_AddFalseToObject(root,"false");
cJSON_AddBoolToObject(root,"Yes",3);
cJSON_AddBoolToObject(root,0);
out = cJSON_Print(root);
//打印对象
//out = cJSON_PrintUnformatted(root);
//非格式化的打印
//printf("===> 2: %s\n",out);
printf("%s\n",out);
cJSON_Delete(root);
//释放对象所占的内存
free(out);
/* These calls create a cJSON item of the appropriate type. */
extern cJSON *cJSON_CreateNull(void);
extern cJSON *cJSON_CreateTrue(void);
extern cJSON *cJSON_CreateFalse(void);
extern cJSON *cJSON_CreateBool(int b);
extern cJSON *cJSON_CreateNumber(double num);
extern cJSON *cJSON_CreateString(const char *string);
extern cJSON *cJSON_CreateArray(void);
extern cJSON *cJSON_CreateObject(void);
/* These utilities create an Array of count items. */
extern cJSON *cJSON_CreateIntArray(const int *numbers,int count);
extern cJSON *cJSON_CreateFloatArray(const float *numbers,int count);
extern cJSON *cJSON_CreateDoubleArray(const double *numbers,int count);
extern cJSON *cJSON_CreateStringArray(const char **strings,int count);
/* Append item to the specified array/object. */
extern void cJSON_AddItemToArray(cJSON *array,cJSON *item);
extern void cJSON_AddItemToObject(cJSON *object,cJSON *item);
extern void cJSON_AddItemToObjectCS(cJSON *object,cJSON *item);
示例:
char *out = NULL;
int array[] = {1,2,3,4};
int array1[] = {1,4,5,6};
cJSON *json = NULL;
cJSON *item = NULL;
cJSON *tmp = NULL;
json = cJSON_CreateObject();
//cJSON_AddItemToObject(json,"array",cJSON_CreateIntArray(array,4));
//与下面两行等价
item = cJSON_CreateIntArray(array,4);
cJSON_AddItemToObject(json,item);
out = cJSON_Print(json);
printf("%s\n",out);
printf("=========================\n");
printf("size: %d\n",cJSON_GetArraySize(item));
//输出为4 4个元素
tmp = cJSON_GetArrayItem(json,0);
//获取json中的第一个节点
if (NULL != tmp)
{
printf("ok..\n");
printf("size: %d\n",cJSON_GetArraySize(tmp));
}
else
printf("error...\n");
//此时item数组中的元素有 1 2 3 4 5 6 7
printf("=========================\n");
cJSON_AddItemToArray(item,cJSON_CreateNumber(5));
cJSON_AddItemToArray(item,cJSON_CreateNumber(6));
cJSON_AddItemToArray(item,cJSON_CreateNumber(7));
printf("size: %d\n",cJSON_GetArraySize(item));
out = cJSON_Print(json);
printf("%s\n",out);
tmp = cJSON_CreateIntArray(array1,6);
//cJSON_AddItemToObject(json,"array1",tmp); //好像与下面一句话等价呢
cJSON_AddItemReferenceToObject(json,tmp);
out = cJSON_Print(json);
printf("%s\n",out);
//整形数组的参考示例
int array3[] = {1,9};
json = cJSON_CreateIntArray(array3,5);
out = cJSON_Print(json);
cJSON_Delete(json);
printf("%s\n",out);
free(out);
//字符指针数组
const char *strings[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
json = cJSON_CreateStringArray(strings,7);
out = cJSON_Print(json);
cJSON_Delete(json);
printf("%s\n",out);
free(out);