1. 程式人生 > >c語言cJSON庫的擴充套件API

c語言cJSON庫的擴充套件API

c語言cJSON庫的功能擴充套件

/*****
 *	設定子節點string ,int
 * ****/
int mcjson_setstring(cJSON *my_json,char *setvalue,char *father,...);
int mcjson_setint(cJSON *my_json,int setvalue,char *father,...);
int mcjson_set_child_obj(cJSON *my_json,void *obj,char *father,...);

/*****
 *	獲取節點的object , string ,int
 * ****/
cJSON *mcJSON_getJSON(cJSON *pj,char *father,...);
char *mcJSON_getSTRING(cJSON *pj,char *return_defvalue,char *father,...);
int mcJSON_getINT(cJSON *pj,int return_defint,char *father,...);

/*
*	讀取整個檔案到字串中
*	清除註釋:預設註釋掉開頭是“//”的行
*/
char *read_json_file_delete_comment(char *filepath);

使用例項 test.json測試資料

{
	"status": 0,
	"msg": "OK",
	"test":{
		"test1":{
			"test2":{
				"id": 0,
				"name": "testnamex"
			}			
		}
	},
	"data": [{
		"id": 1,
		"name": "name1"
	}, {
		"id": 2,
		"name": "name1"
	}]
}

測試範例

int  main(void )
{
    char *filestr;
    cJSON * all_tt;

    if ((filestr =read_json_file_delete_comment("test.json")) == NULL){
        printf("ERROR read file ! \n" );
        exit(EXIT_FAILURE);
    }

    if ((all_tt = cJSON_Parse(filestr)) == NULL){
        printf("ERROR file string to JSON !\n" );
        exit(EXIT_FAILURE);
    }

	printf("test data is :%s\n",cJSON_Print(all_tt));
	printf("child data list :%s\n",cJSON_Print(mcJSON_getJSON(all_tt,"data_list",NULL)));
	printf("test\\test1:%s\n",cJSON_Print(mcJSON_getJSON(all_tt,"test","test1",NULL)));
	printf("test\\test1\\test2\\name value :%s\n",mcJSON_getSTRING(all_tt,NULL,"test","test1","test2","name",NULL));
	printf("test\\test1\\test2\\id value :%d\n",mcJSON_getINT(all_tt,-1,"test","test1","test2","id",NULL));

	mcjson_setstring(all_tt,"text1_value","test","test1","test2" ,NULL);
	mcjson_setint(all_tt, 122,"status",NULL);
    mcjson_setstring(all_tt, "new msg value","msg",NULL);

    printf("test data change now :%s\n",cJSON_Print(all_tt));

	/*資料格式中,沒有嚴格限定TYPE,所以test/test1下的test2 物件, 包含 字串text1_value 和 test2物件 */
	printf("print text1_value position :%s\n",cJSON_Print(mcJSON_getJSON(all_tt,"test",NULL)));
	printf("get name value :%s\n",mcJSON_getSTRING(all_tt,NULL,"test","test1","test2","name",NULL));


	return 0;
}