概述libjson 库允许应用程序解码和编码 JSON 数据用于读取和解释复杂数据或在系统之间传输数据。库提供三大功能领域解码器 (Decoder)读取 JSON 数据导航对象和数组读取和获取各个值的元数据编码器 (Encoder)以逐字段方式写入 JSON 数据获取指向编码器中存储的 JSON 字符串的指针工具函数 (Utility)检索错误信息、清理内存、保存/恢复/重置状态、将格式化的 JSON 数据写入字符数组第一部分JSON 解码器 API类型定义类型描述json_decoder_tJSON 解码器结构体json_decoder_error_tJSON 解码器错误码json_decoder_parse_error_tJSON 解码器解析错误码json_decoder_state_tJSON 解码器状态json_node_type_tJSON 解码器节点类型支持的值类型boolean、numericint, long long, double、string。生命周期函数函数描述json_decoder_create()创建新的 JSON 解码器实例json_decoder_destroy()销毁 JSON 解码器实例json_decoder_cleanup()清理 JSON 解码器实例释放解码器树的内存json_decoder_reset()重置 JSON 解码器实例解析函数函数描述json_decoder_parse_file()解析 JSON 数据文件json_decoder_parse_json_str()解析 JSON 数据字符串导航函数解码器以树结构存储数据可通过推入/弹出对象和数组来导航。函数描述json_decoder_push_array()下降到数组中json_decoder_push_object()下降到对象中json_decoder_pop()从对象或数组中上升json_decoder_next()移动到当前数组或对象中的下一个节点json_decoder_previous()移动到当前数组或对象中的上一个节点json_decoder_index()获取当前节点的索引json_decoder_length()获取当前节点中的元素数量json_decoder_name()获取当前属性的名称json_decoder_position_index()移动到指定索引节点json_decoder_position_property()定位到指定属性值获取函数函数描述json_decoder_get_bool()获取 boolean 值json_decoder_get_int()获取 integer 值json_decoder_get_int_ll()获取long long值json_decoder_get_int_ull()获取unsigned long long值json_decoder_get_double()获取double值json_decoder_get_string()获取 string 值json_decoder_type()获取节点类型json_decoder_get_offset()获取节点值在原始 JSON 字符串中的偏移量类型检查函数函数描述json_decoder_is_integer()判断节点是否表示整数值json_decoder_is_unsigned()判断节点是否表示无符号整数值状态函数函数描述json_decoder_get_state()获取解码器当前状态json_decoder_set_state()设置解码器状态json_decoder_get_status()获取解码器错误状态json_decoder_set_status()设置解码器错误状态json_decoder_free_state()释放之前保存的解码器状态json_decoder_get_parse_error()获取解析错误的位置和原因数据版本控制函数描述json_decoder_get_data_version()获取解码器当前数据版本json_decoder_set_data_version()设置解码器数据版本修改函数函数描述json_decoder_get_encoder()返回用于在解码器树中添加或更新节点的编码器实例json_decoder_remove_node()从解码器树中移除节点调试函数描述json_decoder_dump_tree()转储 JSON 解码器的树内容第二部分JSON 编码器 API类型定义类型描述json_encoder_tJSON 编码器结构体json_encoder_error_tJSON 编码器错误码json_encoder_format_tJSON 编码器数据格式码json_encoder_option_t控制 JSON 编码行为的选项json_encoder_state_tJSON 编码器状态支持的值类型null、boolean、numericint, long long, double、stringC-string 和 JSON 编码字符串。生命周期函数函数描述json_encoder_create()创建新的 JSON 编码器实例json_encoder_destroy()销毁 JSON 编码器实例json_encoder_cleanup()清理 JSON 编码器实例并释放关联内存json_encoder_reset()重置 JSON 编码器实例复合对象函数函数描述json_encoder_start_array()在编码器中开始新数组json_encoder_end_array()结束当前数组json_encoder_start_object()开始新对象json_encoder_end_object()结束当前对象值添加函数函数描述json_encoder_add_null()添加 null 值json_encoder_add_bool()添加 boolean 值json_encoder_add_int()添加 integer 值json_encoder_add_int_ll()添加long long值json_encoder_add_int_ull()添加unsigned long long值json_encoder_add_double()添加 double 值json_encoder_add_string()添加 string 值json_encoder_add_json()添加 JSON 编码的字符串值json_encoder_add_property_name()创建属性名称json_encoder_add_from_decoder()从 JSON 解码器添加项目json_encoder_add_whitespace()在下个名称/值之前添加空白字符缓冲区和状态函数函数描述json_encoder_access_buffer()获取编码器数据缓冲区的读写指针json_encoder_buffer()获取编码器数据缓冲区的只读指针json_encoder_length()获取编码器当前数据长度json_encoder_nesting_level()获取当前嵌套级别json_encoder_reserve_space()在缓冲区中预留指定字节数的空间选项和格式函数描述json_encoder_set_option()设置编码器选项json_encoder_clear_option()清除编码器选项json_encoder_set_format()设置编码器格式状态管理函数描述json_encoder_get_state()获取编码器当前状态json_encoder_set_state()设置编码器状态json_encoder_get_status()获取编码器错误状态json_encoder_set_status()设置编码器错误状态第三部分工具函数这些函数将格式化的 JSON 数据写入输出字符串类似于snprintf()系列函数。对于简单用例比使用完整编码器需要更少代码并按 JSON 规范自动处理特殊字符如换行符和双引号。函数描述jsnprintf()将 JSON 数据写入字符数组最多指定字符数vjsnprintf()将 JSON 数据写入字符数组varargs 版本相关参考C 库参考 — 标准 C 库函数QNX Helpers 开发者指南 — 字符串转换辅助函数