我有以下代码,我想清理由以下命令创建的 json 对象json_object_new_string()
.
#include <json/json.h>
#include <stdio.h>
int main() {
/*Creating a json object*/
json_object * jobj = json_object_new_object();
/*Creating a json string*/
json_object *jstring = json_object_new_string("Joys of Programming");
/*Form the json object*/
json_object_object_add(jobj,"Site Name", jstring);
/*Now printing the json object*/
printf ("The json object created: %sn",json_object_to_json_string(jobj));
/* clean the json object */
json_object_put(jobj);
}
有没有线json_object_put(jobj);
清洁两者jobj
and jstring
?
或者我必须干净jstring
单独与json_object_put(jstring);
?
Edit
问题2
如果jstring
是这样创建成函数的吗?
#include <json/json.h>
#include <stdio.h>
static void my_json_add_obj(json_object *jobj, char *name, char *val) {
/*Creating a json string*/
json_object *jstring = json_object_new_string(val);
/*Form the json object*/
json_object_object_add(jobj,name, jstring);
}
int main() {
/*Creating a json object*/
json_object * jobj = json_object_new_object();
my_json_add_obj(jobj, "Site Name", "Joys of Programming")
/*Now printing the json object*/
printf ("The json object created: %sn",json_object_to_json_string(jobj));
/* clean the json object */
json_object_put(jobj);
}
the jstring
在本例中是函数中的局部变量。是否json_object_put(jobj);
将清洁jstring
(在函数中创建my_json_add_obj()
) ?