Cjson-Demo

cjson文件写入操作

#include 
#include 
#include int main(void)
{//创建空对象cJSON *root = cJSON_CreateObject();cJSON *item = cJSON_CreateObject();cJSON *next = cJSON_CreateObject();cJSON *obj = cJSON_CreateObject();//在root节点下添加cJSON_AddItemToObject(root, "wb", cJSON_CreateNumber(3));cJSON_AddItemToObject(root, "book", cJSON_CreateString("dddd"));cJSON_AddItemToObject(root, "author", cJSON_CreateString("wanger"));//添加数组cJSON *array = NULL;cJSON_AddItemToObject(root, "books", array = cJSON_CreateArray());//添加数组对象for (int i = 0; i < 5; i++){cJSON_AddItemToArray(array, cJSON_CreateNumber(i));}//添加数组对象cJSON_AddItemToArray(array, obj);cJSON_AddItemToObject(obj, "name", cJSON_CreateString("www"));cJSON_AddStringToObject(obj, "type", "123");cJSON_AddItemToArray(array, obj = cJSON_CreateObject());cJSON_AddItemToObject(obj, "name", cJSON_CreateString("eee"));cJSON_AddStringToObject(obj, "type", "456");cJSON_AddItemToObject(root, "sem", item);							//root节点下添加sem节点cJSON_AddItemToObject(item, "slots", next);							//se节点下添加item节点cJSON_AddItemToObject(next, "name", cJSON_CreateString("wangsan")); //添加name节点printf("%s\n", cJSON_Print(root));FILE *fp = fopen("create.json", "w");char *buf = cJSON_Print(root);fwrite(buf, strlen(buf), 1, fp);fclose(fp);cJSON_Delete(root);return 0;
}

Cjson读取

#include 
#include 
#include 
#include void printJson(cJSON * root)//以递归的方式打印json的最内层键值对
{for(int i=0; i<cJSON_GetArraySize(root); i++)   //遍历最外层json键值对{cJSON * item = cJSON_GetArrayItem(root, i);        if(cJSON_Object == item->type)      //如果对应键的值仍为cJSON_Object就递归调用printJsonprintJson(item);else                                //值不为json对象就直接打印出键和值{printf("%s->", item->string);printf("%s\n", cJSON_Print(item));}}
}
int main(int argc, char *argv[])
{FILE *fp = NULL;cJSON *root = NULL;char *out = NULL;char buff[1024] = {0};char json_buff[1024] = {0};int t_buf[64] = {0};fp = fopen("./id.txt", "r");if(NULL == fp){return -1;}while(fgets(buff, sizeof(buff), fp) != NULL){printf("%s", buff);strcpy(json_buff+strlen(json_buff), buff);memset(buff, 0, sizeof(buff));}// fgets(buff, sizeof(buff), fp);root = cJSON_Parse(json_buff);if (!root){printf("Error before: [%s]\n", cJSON_GetErrorPtr());}cJSON * semantic = NULL;cJSON * number = NULL;cJSON * char_values = NULL;cJSON * array_val = NULL;cJSON * array_tmp = NULL;number = cJSON_GetObjectItem(root, "rc");printf("number is %d\n", number->valueint);char_values = cJSON_GetObjectItem(root, "operation");printf("str is %s\n", char_values->string);array_val = cJSON_GetObjectItem(root, "books");int lens = cJSON_GetArraySize(array_val);for(int i=0; i<lens; i++){// array_tmp = cJSON_GetArrayItem(array_val, i);// printf("%d ", array_tmp->valueint);printf("%d ", (cJSON_GetArrayItem(array_val, i))->valueint);t_buf[i] = (cJSON_GetArrayItem(array_val, i))->valueint;}printf("\nlen is %d %d \n", lens, (cJSON_GetArrayItem(array_val,4))->valueint);for(int k = 0; k < 5; k++){printf("%x ", t_buf[k]);}printf("\n");return 0;
}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部