【问题标题】:How can I add null values in JSONC?如何在 JSONC 中添加空值?
【发布时间】:2017-04-03 09:55:41
【问题描述】:

我在 c 中使用 JSONC 库来创建 json。但我不能创建空值。 如何使用 JSONC 库创建以下 json 数据? {“状态”:空}

【问题讨论】:

    标签: json-c


    【解决方案1】:

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

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

    【讨论】:

    • 其实不需要调用_del。 _add 调用将替换任何先前的字段值,即使新值为 NULL。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-17
    • 1970-01-01
    • 2016-12-24
    • 1970-01-01
    • 2015-10-13
    • 2011-05-31
    • 2018-10-04
    相关资源
    最近更新 更多