JSON-C 核心对象操作:json_object_new、get、put 详解

JSON-C 核心对象操作:json_object_new、get、put 详解

【免费下载链接】json-c https://github.com/json-c/json-c is the official code repository for json-c. See the wiki for release tarballs for download. API docs at http://json-c.github.io/json-c/ 【免费下载链接】json-c 项目地址: https://gitcode.com/gh_mirrors/js/json-c

JSON-C 是一个轻量级的 JSON 解析和生成库,广泛应用于 C 语言开发中。本文将详细介绍 JSON-C 中最核心的对象操作函数:json_object_newjson_object_getjson_object_put,帮助开发者快速掌握 JSON 对象的创建、访问和内存管理技巧。

一、创建 JSON 对象:json_object_new 系列函数

JSON-C 提供了丰富的 json_object_new 系列函数,用于创建不同类型的 JSON 对象。这些函数是构建 JSON 数据结构的基础,支持字符串、数字、布尔值、数组和对象等所有 JSON 标准类型。

1.1 基础类型创建

  • 字符串类型:使用 json_object_new_stringjson_object_new_string_len 创建字符串对象。

    struct json_object *str_obj = json_object_new_string("Hello JSON-C");
    
  • 数字类型:支持整数、长整数、无符号长整数和双精度浮点数。

    struct json_object *int_obj = json_object_new_int(42);
    struct json_object *int64_obj = json_object_new_int64(9223372036854775807LL);
    struct json_object *uint64_obj = json_object_new_uint64(18446744073709551615ULL);
    struct json_object *double_obj = json_object_new_double(3.1415926);
    
  • 布尔类型:通过 json_object_new_boolean 创建,参数为 1(true)或 0(false)。

    struct json_object *bool_obj = json_object_new_boolean(1);
    
  • 空值类型:使用 json_object_new_null 创建 null 对象。

    struct json_object *null_obj = json_object_new_null();
    

1.2 复杂类型创建

  • 数组类型:通过 json_object_new_array 创建数组对象,可使用 json_object_array_add 添加元素。

    struct json_object *array_obj = json_object_new_array();
    json_object_array_add(array_obj, json_object_new_string("element1"));
    
  • 对象类型:使用 json_object_new_object 创建键值对对象,通过 json_object_object_add 添加属性。

    struct json_object *obj = json_object_new_object();
    json_object_object_add(obj, "name", json_object_new_string("JSON-C"));
    json_object_object_add(obj, "version", json_object_new_double(0.17));
    

二、访问 JSON 对象:json_object_get 系列函数

创建 JSON 对象后,需要通过 json_object_get 系列函数获取其值或类型信息。这些函数确保在访问前进行类型检查,避免运行时错误。

2.1 获取基本类型值

  • 获取类型:使用 json_object_get_type 确定对象类型,返回值为 json_type 枚举(如 json_type_stringjson_type_int 等)。

    enum json_type type = json_object_get_type(obj);
    
  • 获取字符串json_object_get_string 返回字符串指针,json_object_get_string_len 返回字符串长度。

    const char *str = json_object_get_string(str_obj);
    int len = json_object_get_string_len(str_obj);
    
  • 获取数字:根据对象类型选择对应的获取函数。

    int int_val = json_object_get_int(int_obj);
    int64_t int64_val = json_object_get_int64(int64_obj);
    double double_val = json_object_get_double(double_obj);
    
  • 获取布尔值json_object_get_boolean 返回 json_bool 类型(1 或 0)。

    json_bool bool_val = json_object_get_boolean(bool_obj);
    

2.2 访问复杂类型

  • 数组操作json_object_get_array 获取数组对象,结合 json_object_array_get_idx 访问元素。

    struct array_list *arr = json_object_get_array(array_obj);
    struct json_object *elem = json_object_array_get_idx(array_obj, 0);
    
  • 对象操作json_object_get_object 获取对象的键值对表,通过 json_object_object_get 按 key 访问值。

    struct lh_table *table = json_object_get_object(obj);
    struct json_object *version = json_object_object_get(obj, "version");
    

三、内存管理:json_object_put 函数

JSON-C 采用引用计数机制管理内存,json_object_put 函数用于释放对象引用。每次调用 json_object_newjson_object_get 会增加引用计数,调用 json_object_put 则减少计数,当计数为 0 时对象被自动释放。

3.1 基本用法

  • 创建对象后,使用完毕需调用 json_object_put 释放内存。

    struct json_object *obj = json_object_new_string("test");
    // 使用 obj
    json_object_put(obj); // 释放引用
    
  • 传递对象时,若需要保留引用,需先调用 json_object_get 增加计数。

    struct json_object *obj = json_object_new_int(100);
    struct json_object *arr = json_object_new_array();
    json_object_get(obj); // 增加引用计数
    json_object_array_add(arr, obj); // 将 obj 添加到数组
    json_object_put(obj); // 释放当前作用域的引用,数组仍持有引用
    

3.2 常见内存问题

  • 内存泄漏:忘记调用 json_object_put 会导致内存泄漏。
  • 悬垂指针:释放后继续访问对象会导致未定义行为。
  • 重复释放:多次调用 json_object_put 会导致引用计数异常。

四、综合示例:构建与解析 JSON 对象

以下示例展示如何创建一个复杂 JSON 对象,访问其内容,并正确管理内存:

#include <json-c/json.h>

int main() {
    // 创建对象
    struct json_object *user = json_object_new_object();
    json_object_object_add(user, "name", json_object_new_string("Alice"));
    json_object_object_add(user, "age", json_object_new_int(30));
    
    // 创建数组
    struct json_object *hobbies = json_object_new_array();
    json_object_array_add(hobbies, json_object_new_string("reading"));
    json_object_array_add(hobbies, json_object_new_string("coding"));
    json_object_object_add(user, "hobbies", hobbies);
    
    // 访问数据
    struct json_object *name_obj = json_object_object_get(user, "name");
    printf("Name: %s\n", json_object_get_string(name_obj));
    
    // 释放内存
    json_object_put(user); // 递归释放所有子对象
    return 0;
}

五、最佳实践与注意事项

  1. 类型检查:访问对象前务必使用 json_object_get_type 验证类型,避免类型错误。
  2. 引用计数:传递对象时注意引用计数变化,避免悬垂指针或内存泄漏。
  3. 错误处理:复杂操作中建议检查返回值,例如 json_object_object_get 可能返回 NULL。
  4. 性能优化:对于大型 JSON 数据,优先使用 json_object_new_string_len 避免 strlen 计算。

通过掌握 json_object_newjson_object_getjson_object_put 这三个核心函数,开发者可以高效地在 C 语言中处理 JSON 数据。更多高级功能可参考 JSON-C 官方文档或源码中的测试用例(如 tests/test_json_pointer.c)。

【免费下载链接】json-c https://github.com/json-c/json-c is the official code repository for json-c. See the wiki for release tarballs for download. API docs at http://json-c.github.io/json-c/ 【免费下载链接】json-c 项目地址: https://gitcode.com/gh_mirrors/js/json-c

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值