c++ cjson 用法
数据结构:
typedef struct cJSON { struct cJSON *next,*prev; /* 遍历数组或对象链的前向或后向链表指针*/ struct cJSON *child; /*数组或对象的孩子节点*/ int type; /* key的类型*/ char *valuestring; /*字符串值*/ int valueint; /* 整数值*/ double valuedouble; /* 浮点数值*/ char *string; /* key的名字*/ } cJSON;
(示例代码,数据,api 在下方)
1、添加数据
cJSON_AddNullToObject(object,name) == cJSON_AddItemToObject(object, name, cJSON_CreateNull()) cJSON_AddTrueToObject(object,name) == cJSON_AddItemToObject(object, name, cJSON_CreateTrue()) cJSON_AddFalseToObject(object,name) == cJSON_AddItemToObject(object, name, cJSON_CreateFalse()) cJSON_AddBoolToObject(object,name,b) == cJSON_AddItemToObject(object, name, cJSON_CreateBool(b)) cJSON_AddNumberToObject(object,name,n) == cJSON_AddItemToObject(object, name, cJSON_CreateNumber(n)) cJSON_AddStringToObject(object,name,s) == cJSON_AddItemToObject(object, name, cJSON_CreateString(s))
//添加一条值 cJSON_AddItemToObject(JsonMain, "blessingAddOne", cJSON_CreateString("happy new year")); cJSON_AddStringToObject(JsonMain, "blessingAddTwo", "happy new year 2024");
2、删除数组里面的值
cJSON_DeleteItemFromArray(cJSON *Array,index);
cJSON_DeleteItemFromObject(cJSON *object,"years");
// Array 要删除的数组,index 要删除的索引
cJSON_DeleteItemFromArray(TempPtrArray,0);
cJSON_DeleteItemFromObject(JsonMain,"years");
3、修改字典的值
cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem);
cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON * const item, cJSON * replacement);
cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem);
cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem);
cJSON_ReplaceItemInObjectCaseSensitive(cJSON *object,const char *string,cJSON *newitem);
cJSON_SetValuestring(objectItem,value);
//先获取object TempPtrFriend = cJSON_GetArrayItem(TempPtrArray,1); TempPtrName = cJSON_GetObjectItem(TempPtrFriend,"name"); cJSON_SetValuestring(TempPtrName,"han meimei"); or cJSON_ReplaceItemInObject(TempPtrFriend,"name",cJSON_CreateString("小明"));
4、查询数据:
cJSON_GetObjectItem(const cJSON * const object, const char * const string);
cJSON_GetArrayItem(cJSON *array,int index);
cJSON_GetStringValue(cJSON *item);//获取string value
cJSON_GetIntValue(cJSON *item);//获取int value
cJSON_GetDoubleValue(cJSON *item);//获取double value
//获取第二个数组,因为索引从0开始计算,所以参数为1 TempPtrFriend = cJSON_GetArrayItem(TempPtrArray,1); //获取name节点 TempPtrName = cJSON_GetObjectItem(TempPtrFriend,"name"); //获取数据 name = cJSON_GetStringValue(TempPtrName); or name = TempPtrName->valuestring;
5、转为字符串
cJSON_PrintUnformatted(root);
const char *strData = result->data.c_str(); cJSON *root = cJSON_Parse(strData); //修改完root的值后,重新赋值 char* newStr = cJSON_PrintUnformatted(root); result->data = newStr; cJSON_free(newStr); cJSON_Delete(root);
代码:
数据:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
{ "weight": 89.54, "height": 178, "years": 22, "like": "soccer,basketball", "name": "fool", "man": true, "adult": false, "season": [ "spring", "summer", "fall", "winter" ], "money": null, "child": { "girlfriend": "june", "boyfriend": null }, "friends": [ { "name": "sam" }, { "name": "tom", "age": "17" } ] }
代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<stdio.h> 2 #include <stdlib.h> 3 #include<unistd.h> 4 #include <string.h> 5 #include <errno.h> 6 #include <sys/types.h> 7 #include <sys/stat.h> 8 #include <fcntl.h> 9 #include "lib/cjson/include/cjson/cJSON.h" 10 #include <iostream> 11 static char* cfgPath = "./test.txt"; 12 13 int main(int argc, char** argv) 14 { 15 char Buf[4096] = {0}; 16 int bfd = open(cfgPath,O_RDWR); 17 read(bfd,Buf,4095); 18 fprintf(stdout,"## read json Buf:[%s]",Buf); 19 close(bfd); 20 cJSON* TempPtrArray = NULL; 21 cJSON* TempPtrName = NULL; 22 cJSON* TempPtrFriend = NULL; 23 char * str,* name; 24 double weight; 25 int height; 26 27 //解析我们读到的Json字符串 28 cJSON* JsonMain = cJSON_Parse(Buf); 29 if( NULL == JsonMain ){ 30 fprintf(stdout,"\n## Parse Value Error Exit !!! \n"); 31 return 0; 32 } 33 34 //定位数组,因为他属于itmes节点,所以找到items节点,它的值就是数组了 35 TempPtrArray = cJSON_GetObjectItem(JsonMain,"friends"); 36 37 //定位第二个数组,因为索引从0开始计算,所以参数为1 38 TempPtrFriend = cJSON_GetArrayItem(TempPtrArray,1); 39 40 //定位到nm节点,并修改nm节点的数值 41 TempPtrName = cJSON_GetObjectItem(TempPtrFriend,"name"); 42 43 //更改json 字典的value; 44 cJSON_SetValuestring(TempPtrName,"li lei"); 45 str= cJSON_Print(TempPtrArray); 46 fprintf(stdout,"\n## After Change Name, Friend : \n %s \n",str); 47 48 //添加一条数据 49 cJSON_AddItemToObject(TempPtrFriend, "blessingAddOne", cJSON_CreateString("happy new year 2023")); 50 cJSON_AddStringToObject(TempPtrFriend, "blessingAddTwo", "happy new year 2024"); 51 52 str= cJSON_Print(TempPtrArray); 53 fprintf(stdout,"\n## After Add blessingAddOne, Friend : \n %s \n",str); 54 55 //删除years的节点 56 cJSON_DeleteItemFromObject(JsonMain,"years"); 57 58 //定位到like的节点,并修改其值 59 cJSON* TempPtr = cJSON_GetObjectItem(JsonMain,"like"); 60 61 // 修改节点的数据 62 cJSON_SetValuestring(TempPtr,"swimming"); 63 str= cJSON_Print(TempPtrArray); 64 fprintf(stdout,"\n## After Change like, Friend : \n %s \n",str); 65 66 // 更新节点 67 cJSON_ReplaceItemInObject(TempPtrFriend,"name",cJSON_CreateString("小明")); 68 str= cJSON_Print(TempPtrArray); 69 fprintf(stdout,"\n## After Replace name, Friend : \n %s \n",str); 70 71 cJSON_ReplaceItemInObjectCaseSensitive(TempPtrFriend,"name",cJSON_CreateString("han meimei")); 72 str= cJSON_Print(TempPtrArray); 73 fprintf(stdout,"\n## After Replace name, Friend : \n %s \n",str); 74 75 //删除Array 的第一条数据 76 cJSON_DeleteItemFromArray(TempPtrArray,0); 77 78 // 提取出来json的value 79 name = cJSON_GetStringValue(TempPtrName); 80 fprintf(stdout,"\n## Get TempPtrName name value : %s \n",name); 81 82 if(TempPtrName && TempPtrName->type == cJSON_String){ 83 name = TempPtrName->valuestring; 84 fprintf(stdout,"\n## Get TempPtrName name1 value : %s \n",name); 85 } 86 87 // 获取身高 88 cJSON* TempPtrHeight = cJSON_GetObjectItem(JsonMain,"height"); 89 height = TempPtrHeight->valueint; 90 fprintf(stdout,"\n## Get TempPtrHeight height value : %d \n",height); 91 92 // 获取体重 93 cJSON* TempPtrWeight = cJSON_GetObjectItem(JsonMain,"weight"); 94 weight = TempPtrWeight->valuedouble; 95 fprintf(stdout,"\n## Get TempPtrWeight weight value : %lf \n",weight); 96 97 str= cJSON_Print(TempPtrArray); 98 fprintf(stdout,"\n## After Delete TempPtrmArray index 0, Friend : \n %s \n",str); 99 //fprintf(stdout,"## result TempPtrArray :\n%s\n",str1); 100 101 str= cJSON_Print(JsonMain); 102 fprintf(stdout,"\n## result jsonMain :\n%s\n",str); 103 //释放资源 104 cJSON_Delete(JsonMain); 105 cJSON_free(str); 106 return 0; 107 }
编译:
g++ delete_array.cpp lib/cjson/include/cjson/cJSON.h -L./lib/cjson/lib64/ -lcjson -Wl,-rpath=./lib/cjson/lib64
主要的api:
cJSON API | 说明 |
cJSON_Version() | 获得cJSON的版本 |
cJSON_InitHooks(); | 初始化cJSON_Hooks结构体 |
cJSON_Parse(); | 将字符串解析成cJSON结构体 |
cJSON_ParseWithOpts() | 使用一些配置解析字符串 |
cJSON_Print() | 将cJSON结构体转换成格式化的字符串 |
cJSON_PrintUnformatted() | 将cJSON结构体转换成未格式化的字符串 |
cJSON_PrintBuffered() | 将cJSON结构体使用buffer的字符串,格式化可选 |
cJSON_PrintPreallocated() | 将cJSON结构体使用预分配的内存的字符串,格式化可选 |
cJSON_Delete() | 删除cJSON结构体 |
cJSON_GetArraySize() | 返回Array类型的大小,对Object类型也是有效的 |
cJSON_GetArrayItem() | 返回Array类型的index的值,对Object类型也有效 |
cJSON_GetObjectItem() | 使用key获得对应的value |
cJSON_GetObjectItemCaseSensitive() | 使用对大小写敏感的key获得对应的value |
cJSON_HasObjectItem() | 判断是否ObjectItem存在 |
cJSON_GetErrorPtr() | 获得错误信息 |
类型判断 | |
cJSON_IsInvalid() | 类型判断 |
cJSON_IsFalse() | 类型判断 |
cJSON_IsTrue() | 类型判断 |
cJSON_IsBool() | 类型判断 |
cJSON_IsNull() | 类型判断 |
cJSON_IsNumber() | 类型判断 |
cJSON_IsString() | 类型判断 |
cJSON_IsArray() | 类型判断 |
cJSON_IsObject() | 类型判断 |
cJSON_IsRaw() | 类型判断 |
创造对应类型的cJSON | |
cJSON_CreateNull() | 创造对应类型的cJSON |
cJSON_CreateTrue() | 创造对应类型的cJSON |
cJSON_CreateFalse() | 创造对应类型的cJSON |
cJSON_CreateBool() | 创造对应类型的cJSON |
cJSON_CreateNumber() | 创造对应类型的cJSON |
cJSON_CreateString() | 创造对应类型的cJSON |
cJSON_CreateRaw() | 创造对应类型的cJSON |
cJSON_CreateArray() | 创造对应类型的cJSON |
cJSON_CreateObject() | 创造对应类型的cJSON |
批量创造对应类型的cJSON | |
cJSON_CreateIntArray() | 批量创造对应类型的cJSON |
cJSON_CreateFloatArray() | 批量创造对应类型的cJSON |
cJSON_CreateDoubleArray() | 批量创造对应类型的cJSON |
cJSON_CreateStringArray() | 批量创造对应类型的cJSON |
cJSON_AddItemToArray() | 在指定Array后面增加Item |
cJSON_AddItemToObject() | 在指定Object后面增加Item |
cJSON_AddItemToObjectCS() | 在指定Object后面增加const Item |
cJSON_AddItemReferenceToArray() | 在指定Array后面增加Item引用 |
cJSON_DetachItemViaPointer() | 通过指针从Array删除Item的引用 |
cJSON_DetachItemFromArray() | 从Array删除Item的引用 |
cJSON_DeleteItemFromArray() | 从Array删除Item |
cJSON_DetachItemFromObject() | 从Object删除Item的引用 |
cJSON_DetachItemFromObjectCaseSensitive() | 大小写敏感的从Object删除Item的引用 |
cJSON_DeleteItemFromObject() | 从Object删除Item |
cJSON_DeleteItemFromObjectCaseSensitive() | 大小写敏感的从Object删除Item |
cJSON_InsertItemInArray() | 在Array指定位置插入Item |
cJSON_ReplaceItemViaPointer() | 使用指针替代Item |
cJSON_ReplaceItemInArray() | 替换Array的Item |
cJSON_ReplaceItemInObject() | 替换Object的Item |
cJSON_ReplaceItemInObjectCaseSensitive() | 大小写敏感的替换Object的Item |
cJSON_Duplicate() | 复制cJSON结构体 |
cJSON_Compare() | 比较两个cJSON结构体 |
cJSON_Minify() | 将格式化的字符串压缩 |
cJSON_AddNullToObject() | 调用cJSON_AddItemToObject和cJSON_CreateNull |
cJSON_AddTrueToObject() | 调用cJSON_AddItemToObject和cJSON_CreateTrue |
cJSON_AddFalseToObject() | 调用cJSON_AddItemToObject和cJSON_CreateFalse |
cJSON_AddBoolToObject() | 调用cJSON_AddItemToObject和cJSON_CreateBool |
cJSON_AddNumberToObject() | 调用cJSON_AddItemToObject和cJSON_CreateNumber |
cJSON_AddStringToObject() | 调用cJSON_AddItemToObject和cJSON_CreateString |
cJSON_AddRawToObject() | 调用cJSON_AddItemToObject和cJSON_CreateRaw |
cJSON_SetIntValue() | 设置int的值,同时也设置double的值 |
cJSON_SetNumberValue() | 后台会调用cJSON_SetNumberHelper |
cJSON_SetNumberHelper() | 设置cJSON的number类型的值 |
cJSON_malloc() | cJSON的malloc函数,调用malloc函数 |
cJSON_free() | cJSON的free函数,调用free函数 |