c/c++语言开发共享如何清理“json_object_new_string”创建的json对象?

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

 #include  #include  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

或者我必须使用json_object_put(jstring);单独使用干净的jstring json_object_put(jstring);

编辑

问题2

如果以这种方式将jstring创建为函数,那将会是什么行为?

 #include  #include  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()创建)?

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

      以上就是c/c++开发分享如何清理“json_object_new_string”创建的json对象?相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/c-cdevelopment/544925.html

      (0)
      上一篇 2021年1月9日
      下一篇 2021年1月9日

      精彩推荐