I am trying to create a temporary database using json-c. For that i have created a simple function which return a json object with default value as key:{"value":data} pair and then i am trying to update value of existing signals in database.But here i am getting segmentation fault when i have tried to print database after value update. I have no idea what is wrong here.
#include <stdio.h>
#include <string.h>
#include <json-c/json.h>
#include <stdlib.h>
static int db_created =0;
struct json_object* create_db(){
struct json_object* obj;
struct json_object* val;
obj = json_object_new_object();
val = json_object_new_object();
json_object_object_add(val,"val",json_object_new_int(1001));
json_object_object_add(obj,"key1",val);
json_object_object_add(val,"val",json_object_new_int(1002));
json_object_object_add(obj,"key2",val);
json_object_object_add(val,"val",json_object_new_int(1003));
json_object_object_add(obj,"key3",val);
json_object_object_add(val,"val",json_object_new_int(1004));
json_object_object_add(obj,"key4",val);
json_object_object_add(val,"val",json_object_new_int(1005));
json_object_object_add(obj,"key5",val);
return obj;
}
void run(){
struct json_object* db;
struct json_object* val;
struct json_object* temp;
struct json_object *db2;
val = json_object_new_object();
if(db_created == 1){
printf("database exist\n");
}else{
db = create_db();
printf("database = %s\n",json_object_to_json_string(db));
db_created = 1;
}
//Get a value for key1
json_object_object_get_ex(db,"key1",&val);
printf("value = %s\n",json_object_to_json_string(val));
//update the value for key1
json_object_object_add(val,"val",json_object_new_int(100));
json_object_object_add(db,"ke1",val);
//print database after updating key
printf("database = %s\n",json_object_to_json_string(db));
}
int main(){
run();
return 0;
}
Could not reproduce the segmentation fault
. Please provide more details, such as the compiler version, build command line, OS version, json-c version.
By the way, your database building
has at least one problem: You reused the in the function. function 中的 。 So the value of the previous key will always be overwritten by next writing. So your first database print will have output like this:
database = {
"key1": {
"val": 1005
},
"key2": {
"val": 1005
},
"key3": {
"val": 1005
},
"key4": {
"val": 1005
},
"key5": {
"val": 1005
}
}
The simple mitigation is creating a new object for each of your database item.
struct json_object *create_db() {
struct json_object *obj;
struct json_object *val;
obj = json_object_new_object();
val = json_object_new_object();
json_object_object_add(val, "val", json_object_new_int(1001));
json_object_object_add(obj, "key1", val);
val = json_object_new_object();
json_object_object_add(val, "val", json_object_new_int(1002));
json_object_object_add(obj, "key2", val);
val = json_object_new_object();
json_object_object_add(val, "val", json_object_new_int(1003));
json_object_object_add(obj, "key3", val);
val = json_object_new_object();
json_object_object_add(val, "val", json_object_new_int(1004));
json_object_object_add(obj, "key4", val);
val = json_object_new_object();
json_object_object_add(val, "val", json_object_new_int(1005));
json_object_object_add(obj, "key5", val);
return obj;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.