在C語(yǔ)言中,我們可以通過(guò)以下方法清空J(rèn)SON:
json_object_clear(json_object *obj);
以上函數(shù)可以清空傳入的JSON對(duì)象,使其不包含任何屬性或元素。下面是一個(gè)示例:
#include <stdio.h> #include <json-c/json.h> int main() { //創(chuàng)建JSON對(duì)象 json_object *myobj = json_object_new_object(); //向JSON對(duì)象中添加鍵值對(duì) json_object_object_add(myobj, "name", json_object_new_string("Tom")); json_object_object_add(myobj, "age", json_object_new_int(20)); //輸出JSON對(duì)象 printf("Before clear:\n%s\n", json_object_to_json_string(myobj)); //清空J(rèn)SON對(duì)象 json_object_clear(myobj); //輸出清空后的JSON對(duì)象 printf("After clear:\n%s\n", json_object_to_json_string(myobj)); return 0; }
運(yùn)行以上代碼后,輸出結(jié)果如下:
Before clear: {"name": "Tom", "age": 20} After clear: {}
可以看到,清空后的JSON對(duì)象變成了空的{},不包含任何屬性或元素。