【问题标题】:modify json file using cJSON library使用 cJSON 库修改 json 文件
【发布时间】:2017-03-22 19:00:48
【问题描述】:

我想要做的是读取一个 json 格式的文件并对其进行修改,然后将修改后的内容写入文件。

 55     cJSON *root,*basicpara;
 56     char *out;
 57 
 58     root = dofile("basicparameter.cfg");
 59     out = cJSON_Print(root);
 60     printf("before modify:%s\n",out);
 61     free(out);
 62     basicpara = cJSON_GetObjectItem(root,"basicparameter");
 63     cJSON_GetObjectItem(basicpara,"mode")->valueint = 0;
 64     cJSON_GetObjectItem(basicpara,"TimeoutPoweron")->valueint = 10;
 65 
 66     out = cJSON_Print(root);
 67     printf("after modify:%s\n",out);
 68     free(out);
 69     //write_file("basicparameter.cfg",out);
 70     cJSON_Delete(root);

我很困惑为什么两个内容都一样...

before modify:{
    "basicparameter":   {
        "mode": 1,
        "nBefore":  2,
        "nAfter":   2,
        "LuxAutoOn":    50,
        "LuxAutoOff":   16,
        "TimeoutPoweron":   30
    }
}
after modify:{
    "basicparameter":   {
        "mode": 1,
        "nBefore":  2,
        "nAfter":   2,
        "LuxAutoOn":    50,
        "LuxAutoOff":   16,
        "TimeoutPoweron":   30
    }
}

【问题讨论】:

  • 我知道这是一个老问题,但你能告诉我你是如何真正阅读你的 JSON 文件内容的吗?什么是dofile函数,basicparameter.cfg是什么文件?

标签: cjson


【解决方案1】:

请使用cJSON_SetNumberValue 宏来设置数字。问题是,您只设置了valueint 属性,但打印依赖于valuedouble 属性。

在 cJSON 中同时使用 valueintvaluedouble 是一个糟糕的设计决策,将来可能也会让很多人感到困惑。

【讨论】:

    猜你喜欢
    • 2014-06-15
    • 2020-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-11
    • 1970-01-01
    相关资源
    最近更新 更多