拷備.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;
}