cjson文件写入操作
#include <stdio.h>
#include <string.h>
#include <cjson/cJSON.h>
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 <stdio.h>
#include <stdlib.h>
#include <cjson/cJSON.h>
#include <string.h>
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就递归调用printJson
printJson(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;
}