/*************************************************
* example json file
* name file
* {
* "firstname":"lin",* "lastname":"spock",* "age":"25",* "address":{
* "city": "New York",* "country":"USA",* "street":["11","22","33"]
* },* "hobbies":["basketball","football"],* "new":["new"]
* }
*************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "cJSON.h"
void Set(cJSON *root,char *str3,char *str4,char *str5,char *str6) { /*json filename set fathercjson childcjson newvalue*/
if (strcmp(str3,"root") == 0) { /*jsona file set root firstname chen*/
cJSON *tmp_json = cJSON_GetObjectItem(root,str4);
if (tmp_json != NULL) {
cJSON_ReplaceItemInObject(root,str4,cJSON_CreateString(str5));
} else {
fprintf(stderr,"%s元素不存在,請先添加\n",str4);
fprintf(stdout,"新增元素命令格式:程序 json文件名 add object|array 父元素 子元素 新值\n");
exit(0);
}
} else {
cJSON *tmp_json = cJSON_GetObjectItem(root,str3);
if (tmp_json != NULL) {
if (tmp_json->type == cJSON_Object) { /*json file set address city Beijing*/
cJSON *obj_json = cJSON_GetObjectItem(tmp_json,str4);
if (obj_json != NULL) {
cJSON_ReplaceItemInObject(tmp_json,cJSON_CreateString(str5));
} else {
fprintf(stderr,"%s元素不存在,请先创建\n",str4);
exit(0);
}
} else if (tmp_json->type == cJSON_Array) { /*json file set hobbies 1 readbook*/
int count = cJSON_GetArraySize(tmp_json);
if (atoi(str4) < (count + 1) && atoi(str4) > 0) {
cJSON_ReplaceItemInArray(tmp_json,atoi(str4) - 1,"该数组不存在第%s个元素,请重新输入\n",str4);
exit(0);
}
}
} else {
fprintf(stderr,str3);
fprintf(stdout,"新增元素命令格式:程序 json文件名 add object|array 父元素 子元素 新值\n");
exit(0);
}
}
}
void Add(cJSON *root,char *str6) { /*json filename add object fathercjson childcjson addvlaue*/
if (strcmp(str3,"object") == 0) {
if (strcmp(str4,"root") == 0) { /*jsona file add object root test test*/
cJSON *tmp_json = cJSON_GetObjectItem(root,str5);
if (tmp_json != NULL) {
fprintf(stderr,"%s元素已存在,請先删除\n",str5);
fprintf(stdout,"删除命令:程序 json文件名 delete 父元素 子元素\n");
exit(0);
} else {
cJSON_AddItemToObject(root,str5,cJSON_CreateString(str6));
}
} else { /*json file add object address planet eirth*/
cJSON *tmp_json = cJSON_GetObjectItem(root,str4);
if (tmp_json != NULL) {
if (tmp_json->type == cJSON_Object) {
cJSON *obj_json = cJSON_GetObjectItem(tmp_json,str5);
if (obj_json != NULL) {
fprintf(stderr,str5);
fprintf(stdout,"删除命令:程序 json文件名 delete 父元素 子元素\n");
exit(0);
} else {
cJSON_AddItemToObject(tmp_json,cJSON_CreateString(str6));
}
} else if (tmp_json->type == cJSON_Array) {
cJSON *obj_json = cJSON_CreateObject();
cJSON_AddItemToArray(tmp_json,obj_json);
cJSON_AddStringToObject(obj_json,str6);
}
} else {
fprintf(stderr,"%s元素不存在,请在上一级先添加%s\n",str4);
exit(0);
}
}
} else if (strcmp(str3,"array") == 0) { /*json filename add array fathercjson childcjson addvalue*/
if (strcmp(str4,"root") == 0) { /*jsona file add array root hobbies vallyball*/
cJSON *tmp_json = cJSON_GetObjectItem(root,str5);
if (tmp_json != NULL) {
cJSON_AddItemToArray(tmp_json,cJSON_CreateString(str6));
} else {
cJSON *tmp_json = cJSON_CreateArray();
cJSON_AddItemToObject(root,tmp_json);
cJSON_AddItemToArray(tmp_json,cJSON_CreateString(str6));
}
} else {
fprintf(stderr,"%s数组下尚不能添加二级数组或对象\n",str4);
exit(0);
/*
cJSON *tmp_json = cJSON_GetObjectItem(root,str4);
cJSON *arr_json = cJSON_GetObjectItem(tmp_json,str5);
if (arr_json != NULL) {
cJSON_AddItemToArray(arr_json,cJSON_CreateString(str6));
} else {
cJSON *arr_json = cJSON_CreateArray();
cJSON_AddItemToObject(tmp_json,arr_json);
cJSON_AddItemToArray(arr_json,cJSON_CreateString(str6));
}
*/
}
} else {
fprintf(stderr,"第三个参数可选值为: object|array,请重新输入\n");
exit(0);
}
}
void Delete(cJSON *root,char *str4) { /*json filename delete fathercjson childcjson*/
if (strcmp(str3,"root") == 0) { /*/json file delete root firstname*/
cJSON *tmp_json = cJSON_GetObjectItem(root,str4);
if (tmp_json != NULL) {
cJSON_DeleteItemFromObject(root,str4);
} else {
fprintf(stderr,"%s元素不存在,请重新输入\n",str4);
exit(0);
}
} else {
cJSON *tmp_json = cJSON_GetObjectItem(root,str3);
if (tmp_json != NULL) { /*json file delete address city*/
if (tmp_json->type == cJSON_Object) {
cJSON *obj_json = cJSON_GetObjectItem(tmp_json,str4);
if (obj_json != NULL) {
cJSON_DeleteItemFromObject(tmp_json,str4);
} else {
fprintf(stderr,str4);
exit(0);
}
} else if (tmp_json->type == cJSON_Array) { /*json file delete hobbies 2*/
int count = cJSON_GetArraySize(tmp_json);
if (atoi(str4) < (count + 1) && atoi(str4) > 0) {
cJSON_DeleteItemFromArray(tmp_json,atoi(str4) - 1);
} else {
fprintf(stderr,str4);
(0);
}
}
} else {
fprintf(stderr,str3);
exit(0);
}
}
}
int main(int argc,char *argv[]) {
FILE *fp = fopen(argv[1],"r"); /*从文件中读取要解析的JSON数据*/
if (fp == NULL) {
fprintf(stderr,"第二个参数为文件名,%s文件打开失败\n");
exit(0);
}
fseek(fp,SEEK_END); /*将指针指向文件末尾*/
long len = ftell(fp); /*ftell获得当前指针位置相对于文件首的偏移字节数*/
fseek(fp,SEEK_SET); /*再将指针指回开头*/
char *data = (char*) malloc(len + 1); /*分配内存*/
fread(data,1,len,fp); /*printf("%s\n",data)*/
fclose(fp);
cJSON *root = cJSON_Parse(data); /*将字符串解析成json结构体*/
char *ro = cJSON_Print(root); /*可通过这个指针输出内容测试*/
if (NULL == root) {
fprintf(stderr,"error:%s\n",cJSON_GetErrorPtr());
cJSON_Delete(root);
}
if (strcmp(argv[2],"set") == 0) { /*改*/
if (argc != 6) {
fprintf(stderr,"参数过少或过多\n");
fprintf(stdout,"set命令参数有6个\n命令格式:程序 json文件名 set 父元素 子元素 新值\n");
exit(0);
}
Set(root,argv[3],argv[4],argv[5],argv[6]);
} else if (strcmp(argv[2],"add") == 0) { /*增*/
if (argc != 7) {
fprintf(stderr,"add命令参数有7个\n命令格式:程序 json文件名 add object|array 父元素 子元素 新值\n");
exit(0);
}
Add(root,"delete") == 0) { /*删*/
if (argc != 5) {
fprintf(stderr,"delete命令参数有五个\n命令格式:程序 json文件名 delete 父元素 子元素\n");
exit(0);
}
Delete(root,argv[4]);
} else {
fprintf(stderr,"第三个参数错误\n"
"第三个参数为:set | add | delete\n");
fprintf(stdout,"具体命令格式为:\n"
"修改:程序 json文件名 set 父元素 子元素 新值\n"
"添加:程序 json文件名 add object|array 父元素 子元素 新值\n"
"删除:程序 json文件名 delete 父元素 子元素\n"
"當所要操作元素在最外層時父元素為\"root\"\n");
exit(0);
}
FILE *file;
file = fopen("file3","wb"); /*将cjson写入文件*/
char *out = cJSON_Print(root);
fprintf(file,"%s",out);
fclose(file);
free(out);
return 1;
}
原文链接:https://www.f2er.com/json/288752.html