测试程序的目的是更新已有的json文件。

下面是测试程序的代码

[cpp] view plain copy
  1. #include "smartlight.h"  
  2. #include "cJSON.h"  
  3.   
  4. cJSON *dofile(char *filename)  
  5. {  
  6.     FILE *f;  
  7.     long len;  
  8.     char *data;  
  9.     cJSON *json,*ret;  
  10.       
  11.     f=fopen(filename,"rb");  
  12.     fseek(f,0,SEEK_END);  
  13.     len=ftell(f);  
  14.     fseek(f,0,SEEK_SET);  
  15.     data=(char*)malloc(len+1);  
  16.     fread(data,1,len,f);  
  17.     data[len]='\0';  
  18.     json=cJSON_Parse(data);  
  19.     if (!json)   
  20.     {  
  21.         printf("Error before: [%s]\n",cJSON_GetErrorPtr());  
  22.         ret = NULL;  
  23.         goto EXIT;  
  24.     }  
  25.     else  
  26.     {  
  27.         //printf("%s\n",data);  
  28.         ret = json;  
  29.     }  
  30.   
  31. EXIT:  
  32.     free(data);  
  33.     fclose(f);  
  34.     return ret;  
  35. }  
  36.   
  37. int write_file(char *filename,char *out)  
  38. {  
  39.     FILE *fp = NULL;  
  40.   
  41.     fp = fopen(filename,"a+");  
  42.     if(fp == NULL)  
  43.     {  
  44.         fprintf(stderr,"open file failed\n");  
  45.         exit(-1);  
  46.     }  
  47.     fprintf(fp,"%s",out);  
  48.   
  49.     if(fp != NULL)  
  50.         fclose(fp);  
  51. }  
  52.   
  53. int main()  
  54. {  
  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,"mode")->valuedouble = 0;  
  65.     cJSON_GetObjectItem(basicpara,"TimeoutPoweron")->valueint = 10;  
  66.     //cJSON_GetObjectItem(basicpara,"TimeoutPoweron")->valuedouble = 10;  
  67.   
  68.     out = cJSON_Print(root);  
  69.     printf("after modify:%s\n",out);  
  70.     free(out);  
  71.     //write_file("basicparameter.cfg",out);  
  72.     cJSON_Delete(root);  
  73.   
  74.     return 0;  
  75. }  

刚开始只修改了valueint的值,结果显示修改前后的结果一样。

[cpp] view plain copy
  1. cJSON_GetObjectItem(basicpara,"mode")->valueint = 0;  
使用cJSON库更新json文件


然后修改valuedouble的值,才能真能修改json文件。

使用cJSON库更新json文件


如果只调用

[cpp] view plain copy
  1. cJSON_GetObjectItem(basicpara,"mode")->valuedouble = 0;  
也只是修改了valuedouble的值。

所以如果想要修改valueint的值必须同时调用

[cpp] view plain copy
  1. cJSON_GetObjectItem(basicpara,"mode")->valueint = 0;  
  2. cJSON_GetObjectItem(basicpara,"mode")->valuedouble = 0;  

引用链接:https://sourceforge.net/p/cjson/discussion/998970/thread/5c13b93f/

测试程序的目的是更新已有的json文件。

下面是测试程序的代码

[cpp] view plain copy
  1. #include "smartlight.h"  
  2. #include "cJSON.h"  
  3.   
  4. cJSON *dofile(char *filename)  
  5. {  
  6.     FILE *f;  
  7.     long len;  
  8.     char *data;  
  9.     cJSON *json,*ret;  
  10.       
  11.     f=fopen(filename,"rb");  
  12.     fseek(f,0,SEEK_END);  
  13.     len=ftell(f);  
  14.     fseek(f,0,SEEK_SET);  
  15.     data=(char*)malloc(len+1);  
  16.     fread(data,1,len,f);  
  17.     data[len]='\0';  
  18.     json=cJSON_Parse(data);  
  19.     if (!json)   
  20.     {  
  21.         printf("Error before: [%s]\n",cJSON_GetErrorPtr());  
  22.         ret = NULL;  
  23.         goto EXIT;  
  24.     }  
  25.     else  
  26.     {  
  27.         //printf("%s\n",data);  
  28.         ret = json;  
  29.     }  
  30.   
  31. EXIT:  
  32.     free(data);  
  33.     fclose(f);  
  34.     return ret;  
  35. }  
  36.   
  37. int write_file(char *filename,char *out)  
  38. {  
  39.     FILE *fp = NULL;  
  40.   
  41.     fp = fopen(filename,"a+");  
  42.     if(fp == NULL)  
  43.     {  
  44.         fprintf(stderr,"open file failed\n");  
  45.         exit(-1);  
  46.     }  
  47.     fprintf(fp,"%s",out);  
  48.   
  49.     if(fp != NULL)  
  50.         fclose(fp);  
  51. }  
  52.   
  53. int main()  
  54. {  
  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,"mode")->valuedouble = 0;  
  65.     cJSON_GetObjectItem(basicpara,"TimeoutPoweron")->valueint = 10;  
  66.     //cJSON_GetObjectItem(basicpara,"TimeoutPoweron")->valuedouble = 10;  
  67.   
  68.     out = cJSON_Print(root);  
  69.     printf("after modify:%s\n",out);  
  70.     free(out);  
  71.     //write_file("basicparameter.cfg",out);  
  72.     cJSON_Delete(root);  
  73.   
  74.     return 0;  
  75. }  

刚开始只修改了valueint的值,结果显示修改前后的结果一样。

[cpp] view plain copy
  1. cJSON_GetObjectItem(basicpara,"mode")->valueint = 0;  
使用cJSON库更新json文件


然后修改valuedouble的值,才能真能修改json文件。

使用cJSON库更新json文件


如果只调用

[cpp] view plain copy
  1. cJSON_GetObjectItem(basicpara,"mode")->valuedouble = 0;  
也只是修改了valuedouble的值。

所以如果想要修改valueint的值必须同时调用

[cpp] view plain copy
  1. cJSON_GetObjectItem(basicpara,"mode")->valueint = 0;  
  2. cJSON_GetObjectItem(basicpara,"mode")->valuedouble = 0;  

引用链接:https://sourceforge.net/p/cjson/discussion/998970/thread/5c13b93f/

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-30
  • 2021-06-03
  • 2022-12-23
猜你喜欢
  • 2021-07-07
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-08
  • 2021-08-29
相关资源
相似解决方案