拷备.c和.h文件到指定的目录中
构建需要使用的函数
extern cJSON *cJSON_CreateObject(void);
extern void cJSON_AddItemToObject(cJSON *object,const char *string,cJSON *item);
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);
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include "cJSON.h"
int main(int argc, char *argv[])
{
// 创建对象
cJSON* obj = cJSON_CreateObject();
// 创建子对象
cJSON* subObj = cJSON_CreateObject();
// 添加key-value
cJSON_AddItemToObject(subObj,"factory",cJSON_CreateString("一汽大众"));
cJSON_AddItemToObject(subObj,"last",cJSON_CreateNumber(31));
cJSON_AddItemToObject(subObj,"price",cJSON_CreateNumber(83));
cJSON_AddItemToObject(subObj,"sell",cJSON_CreateNumber(49));
cJSON_AddItemToObject(subObj,"sum",cJSON_CreateNumber(80));
// 创建数组
cJSON* array = cJSON_CreateArray();
cJSON_AddItemToArray(array,cJSON_CreateNumber(123));
cJSON_AddItemToArray(array,cJSON_CreateNumber(1));
cJSON_AddItemToArray(array,cJSON_CreateString("hello,world"));
// 创建对象
cJSON* childObj = cJSON_CreateObject();
cJSON_AddItemToObject(childObj,"梅塞奔驰",cJSON_CreateString("心所向,持以恒"));
cJSON_AddItemToArray(array,childObj);
cJSON_AddItemToObject(subObj,"othre",array);
// obj中添加key - value
cJSON_AddItemToObject(obj,"奔驰",subObj);
// 数据格式化
char* data = cJSON_Print(obj);
FILE* fp = fopen("car.json","w");
fwrite(data,sizeof(char),strlen(data)+1,fp);
fclose(fp);
return 0;
}
gcc *.c -o app -lm -I./include
{
"奔驰": {
"factory": "一汽大众",
"last": 31,
"price": 83,
"sell": 49,
"sum": 80,
"othre": [123, 1, "hello,world", {
"梅塞奔驰": "心所向,持以恒"
}]
}
}
解析需要的函数
/* 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);//获取错误字符串
代码示例
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include "cJSON.h"
int main(int argc, char *argv[])
{
if(argc < 2)
{
printf("./app jsonfile\n");
exit(1);
}
// 加载json文件
FILE* fp = fopen(argv[1],"r");
char buf[1024] = {0};
fread(buf,1,sizeof(buf),fp);
cJSON* root = cJSON_Parse(buf);
cJSON* subobj = cJSON_GetObjectItem(root,"奔驰");
// 判断对象是否存在
if(subobj)
{
printf("奔驰: \n");
// 获取子对象
cJSON* item = NULL;
item= cJSON_GetObjectItem(subobj,"factory");
printf("%s: %s \n",item->string,item->valuestring);
item= cJSON_GetObjectItem(subobj,"last");
printf("last: %d \n",item->valueint);
item= cJSON_GetObjectItem(subobj,"price");
printf("price: %d\n",item->valueint);
item= cJSON_GetObjectItem(subobj,"sell");
printf("sell: %d\n",item->valueint);
item= cJSON_GetObjectItem(subobj,"sum");
printf("sum: %d\n",item->valueint);
cJSON *other = NULL;
other = cJSON_GetObjectItem(subobj,"othre");
// if(cJSON_IsArray(other))
printf("other: \n");
if(cJSON_Array == other->type)
{
for(int i = 0;i<cJSON_GetArraySize(other);++i)
{
item = cJSON_GetArrayItem(other,i);
if(cJSON_IsNumber(item))
printf(" %d \n",item->valueint);
else if(cJSON_IsString(item))
//else if(cJSON_IsObject(item))
{
cJSON* temp = cJSON_GetArrayItem(other,i);
printf(" %s \n",temp->valuestring);
}
else if(cJSON_Object == item->type)
{
cJSON* temp = cJSON_GetObjectItem(item,"梅塞奔驰");
printf("%s %s\n",temp->string,temp->valuestring);
}
}
}
}
cJSON_Delete(root);
fclose(fp);
return 0;
}