如何清理由"json_object_new_string"创建的json对象? [英] How to clean a json object created by "json_object_new_string"?

查看:938
本文介绍了如何清理由"json_object_new_string"创建的json对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,我想清除由json_object_new_string()创建的json对象.

I have the following code and I want to clean a json object created by 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);是否同时清洁jobjjstring?

还是我必须独自清洁jstringjson_object_put(jstring);?

Or I have to a clean jstring alone with json_object_put(jstring);?

修改

question2

如果以这种方式将jstring创建为函数,将会发生什么情况?

what will be the behaviour if the jstring is creted into a function in this way?

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

}

在这种情况下,jstring是函数的局部变量. json_object_put(jobj);是否会清理jstring(在函数my_json_add_obj()中创建)?

the jstring in this case is a local variable into a function. Does the json_object_put(jobj); will clean the jstring (created in the function my_json_add_obj()) ?

推荐答案

json_object_put将释放对象引用的所有内容.所以,是的,在jobj上使用该函数释放整个对象就足够了.

json_object_put will free everything referenced by the object. So yes, it's sufficient to use that function on jobj to free the whole object.

这篇关于如何清理由"json_object_new_string"创建的json对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆