我有一个 json 文件。并且,文件已成功加载。但是,我想更改如下值并保存带有修改的 json 文件。但是,该值根本没有改变和保存。我该怎么办?
来自 /home/pi/desktop/test.json
{
"new_one": 1,
"new_two" : "do not",
"new_three" : true
}
到 /home/pi/desktop/test.json
{
"new_one": 234,
"new_two" : "do",
"new_three" : false
}
所以我做了
int main()
{
json_t *json;
json_error_t error;
char *pos;
json_t *obj = json_object();
int rc =0 ;
json = json_load_file("./test.json", 0, &error);
if (!json)
{
fprintf(stderr, "process : json error on line %d: %s\n", error.line, error.text);
rc = 1;
}
const char *key;
json_t *value;
void *iter = json_object_iter( json );
while( iter )
{
key = json_object_iter_key(iter);
value = json_object_iter_value(iter);
if(!strcmp(key, "new_one")){
printf("Change Value\n" );
json_object_set(iter, "new_one", json_integer(1234));
}
if(!strcmp(key, "new_three")){
printf("Change Value\n" );
json_object_set(iter, "new_three", json_string("alert"));
}
iter = json_object_iter_next(json, iter);
}
return 0;
}