2

what i want to do is reading a json format file and modify it then writing the modified content to file.

 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);

i am confused why both contents are the same...

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
    }
}
sagarfan
  • 21
  • 4
  • I know this is a old question but can you tell me how you are actually reading your JSON files content. What is dofile function, what kind of file is basicparameter.cfg? – Ghos3t Feb 16 '18 at 05:24

1 Answers1

1

Please use the cJSON_SetNumberValue macro for setting the number. The problem is, that you are only setting the valueint property but printing relies on the valuedouble property.

Having both valueint and valuedouble in cJSON was a terrible design decision and will probably confuse many people in the future as well.

FSMaxB
  • 2,280
  • 3
  • 22
  • 41