如何在JSONC中添加空值?

时间:2017-04-03 09:55:42

标签: json-c

我在c中使用JSONC库来创建json。但我无法创建空值。 如何使用JSONC库创建以下json数据? {“status”:null}

1 个答案:

答案 0 :(得分:0)

根据json-c库的接口,我想你可以先从对象中删除nameval对,然后添加一个NULL为NULL的字段:

json_object_object_del(jexample, "status");
json_object_object_add(jexample, "status", NULL);

参考: https://github.com/json-c/json-c/blob/master/json_object.h

请参阅API定义:

/*
 * @param obj the json_object instance
 * @param key the object field name (a private copy will be duplicated)
 * @param val a json_object or NULL member to associate with the given field
 *
 * @return On success, <code>0</code> is returned.
 *  On error, a negative value is returned.
 */
    JSON_EXPORT int json_object_object_add(struct json_object* obj, const char *key,
                       struct json_object *val);