修改json对象的值并进行保存

//修改json对象的值并进行保存
static int parse_app_config(char *file)  //file为config.json
{
    char *cfg = read_config_file(file, NULL);
    static char *state_table[] = {
    "words=ni hao xiao le;thresh=0.3;major=1;",  
    "words=ni hao xiao ju;thresh=0.3;major=1;",  
    "words=ni hao xiao bai;thresh=0.3;major=1;"
    };
    if (!cfg) return -1;
    cJSON *js = cJSON_Parse(cfg);
    if (!js) return -1;
    char *string = cJSON_Print(js);
    cJSON *wakeup_js = cJSON_GetObjectItem(js, "wakeup");
    cJSON *words_js = cJSON_GetObjectItem(wakeup_js, "words");
    cJSON *cfg_js = cJSON_GetObjectItem(wakeup_js, "cfg");
    cJSON *env_js = cJSON_GetObjectItem(cfg_js, "env");  
    i = cJSON_GetArraySize(words_js);
    printf("i: %d\n",i);
    cJSON *item = cJSON_GetArrayItem(words_js, 0);
    cJSON *word_js = cJSON_GetObjectItem(item, "word");
    printf("YYYYYYYYYYYYY :%s \n",word_js->valuestring);
        //修改对象的值
   cJSON_ReplaceItemInObject(item,"word",cJSON_CreateString("nihaoxiaobai"));
    cJSON *Replace_js = cJSON_GetObjectItem(item, "word");
    printf("YYYYYYYYYYYYY :%s \n",Replace_js->valuestring);

    char *strin = cJSON_Print(js);
    FILE *fp = fopen("config.json", "wb+");
    fprintf(fp, strin);
    char *cfgh = read_config_fp(fp,NULL);
    printf("%s\n",cfgh);
    cJSON_Delete(js);
    return 0;
}

你可能感兴趣的:(linux)