json-c 理解记录

2023-05-16

1. json-c 理解记录

  • 1. json-c 理解记录
    • 1.1. 编译及说明
    • 1.2. 特色
    • 1.3. 使用
      • 1.3.1. 创建,读写文件
      • 1.3.2. 拷贝
      • 1.3.3. 增改
        • 1.3.3.1. 字典增加元素
        • 1.3.3.2. 数组增加修改元素
      • 1.3.4. 字典数组删除元素
      • 1.3.5. 查
        • 1.3.5.1. 类型
        • 1.3.5.2. 数组
        • 1.3.5.3. 字典
        • 1.3.5.4. 其他标准类型
      • 1.3.6. 排序
      • 1.3.7. other
    • 1.4. 参考地址

1.1. 编译及说明

一个 C 库,多用于嵌入式系统

  • 编译源码及文档
mkdir build
cd build
cmake ..
make
make doc

1.2. 特色

  • json-c 默认不支持多线程,可以通过编译参数 DENABLE_THREADING 开启,但是开启后仅限于 json_object_get 和 json_object_put 函数。据说会慢3倍

1.3. 使用

1.3.1. 创建,读写文件

创建的 json_object 都需要通过 json_object_put 释放

struct json_object *    json_object_new_object (void);  // key:val
struct json_object *    json_object_new_array (void);
struct json_object *    json_object_new_boolean (json_bool b);
struct json_object *    json_object_new_int (int32_t i);
struct json_object *    json_object_new_int64 (int64_t i);
struct json_object *    json_object_new_double (double d);
struct json_object *    json_object_new_double_s (double d, const char *ds);
struct json_object *    json_object_new_string (const char *s);
struct json_object *    json_object_new_string_len (const char *s, int len);
// 字符串到json对象,与之对应的是 json_object_to_json_string 函数
struct json_object *    json_tokener_parse(const char *str);
// read from file
struct json_object*     json_object_from_file(const char *filename);
struct json_object*     json_object_from_fd(int fd);
// write to file
int                     json_object_to_file(const char *filename, struct json_object *obj);
int                     json_object_to_file_ext(const char *filename, struct json_object *obj, int flags);
int                     json_object_to_fd(int fd, struct json_object *obj, int flags);

1.3.2. 拷贝

// 引用拷贝,引用计数+1
json_object * json_object_get (struct json_object *obj);
// 引用释放,引用计数-1,引用计数为0释放内存,返回值为1
int json_object_put (struct json_object *obj);

1.3.3. 增改

1.3.3.1. 字典增加元素

// 字典增加元素
int json_object_object_add (struct json_object *obj, const char *key, struct json_object *val);
// 扩展版本,支持一些选项,用于内存优化,比如 opts=JSON_C_OBJECT_ADD_KEY_IS_NEW 或 JSON_C_OBJECT_KEY_IS_CONSTANT
int json_object_object_add_ex (struct json_object *obj, const char *const key, struct json_object *const val, const unsigned opts);

1.3.3.2. 数组增加修改元素

// 数组增加元素
int json_object_array_add (struct json_object *obj, struct json_object *val);
// 替换数组中元素
int json_object_array_put_idx (struct json_object *obj, size_t idx, struct json_object *val);

1.3.4. 字典数组删除元素

void json_object_object_del (struct json_object *obj, const char *key);
int json_object_array_del_idx(struct json_object *obj, size_t idx, size_t count);

1.3.5. 查

1.3.5.1. 类型

// 类型操作
enum json_type json_object_get_type (const struct json_object *obj);
int json_object_is_type(const struct json_object *obj, enum json_type type);
extern const char *json_type_to_name(enum json_type o_type);

1.3.5.2. 数组

// 数组操作
struct array_list * json_object_get_array (const struct json_object *obj);
size_t json_object_array_length (const struct json_object *obj);
struct json_object * json_object_array_get_idx (const struct json_object *obj, size_t idx);

1.3.5.3. 字典

// 字典操作
int json_object_object_length (const struct json_object *obj);
// 不建议使用,推介使用 json_object_object_get_ex 代替
struct json_object* json_object_object_get(const struct json_object* obj, const char *key);
// 获取同时,判断是否存在,其他也有类似函数,可以查找。这里无需通过 json_object_put 释放
json_bool json_object_object_get_ex(const struct json_object* obj, const char *key, struct json_object **value);
// 格式化成字符串输出
const char * json_object_to_json_string (struct json_object *obj);

1.3.5.4. 其他标准类型

// 其他操作
json_bool json_object_get_boolean (const struct json_object *obj);
int32_t json_object_get_int (const struct json_object *obj);
int64_t json_object_get_int64 (const struct json_object *obj);
double json_object_get_double (const struct json_object *obj);
const char * json_object_get_string (struct json_object *obj);

1.3.6. 排序

// 数组排序,自己写比较函数
void json_object_array_sort (struct json_object *jso, int(*sort_fn)(const void *, const void *));

1.3.7. other

const char *json_util_get_last_err(void);
extern int json_parse_int64(const char *buf, int64_t *retval);
extern int json_parse_double(const char *buf, double *retval);

1.4. 参考地址

  • 源码
  • wiki
  • tutorials
  • JSON解析器json-c
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

json-c 理解记录 的相关文章

随机推荐

  • Oracle snapper ASH监控工具

    Oracle snapper ASH监控工具 snapper工具是由国外技术人员 xff0c 将基于Oracle ash技术原理用来监控数据库会话的负载情况 比较适合小范围时间监控 xff0c 可以生成多个快照 xff0c 例如1小时内 x
  • Matlab之数据筛选

    Matlab功能强大 xff0c 这里介绍一些数据筛选方法 xff0c 至少让其达到Excel的数据筛选程度 一 从多维数组中取某些行或列组合为新数组 示例如下 xff1a 取某些列组成新数组 newdata span class toke
  • kurento-room的搭建教程,绝对可行

    目前网上参考的kurento room的搭建教程 xff0c 比如https blog csdn net u010602143 article details 106670864 已经跑不起了 我估计原来也跑不起 原因很简单 xff0c k
  • Python 爬取携程所有机票

    打开携程网 xff0c 查询机票 xff0c 如广州到成都 这时网址为 xff1a http flights ctrip com booking CAN CTU day 1 html DDate1 61 2018 06 15 其中 xff0
  • Rust Web框架warp使用

    目录 简介快速开始Request和Response从path和body中获取参数从query中获取参数 设置状态码 静态文件 目录websocket重定向tls 简介 warp是一个超级便捷 可组合 速度极快的异步Web框架 目前最新版本为
  • CCNP路由实验之四 动态路由协议之EIGRP

    CCNP 路由实验之四 动态路由协议之 EIGRP 动态路由协议可以自动的发现远程网络 xff0c 只要网络拓扑结构发生了变化 xff0c 路由器就会相互交换路由信息 xff0c 不仅能够自动获知新增加的网络 xff0c 还可以在当前网络连
  • C++中typedef用法说明

    typedef声明提供了一种将标识符声明为类型别名的方法 xff0c 用于替换复杂的类型名 解释 在声明中使用typedef说明符时 xff0c 会指定这个声明是typedef声明 xff0c 而不是变量或函数声明 通常 xff0c typ
  • Ubuntu 服务配置(sysv-rc-conf)

    版权声明 xff1a 本文为博主原创文章 xff0c 未经博主允许不得转载 sudo apt get install sysv rc conf sudo sysv rc conf 运行级别说明 xff1a S表示开机后就会运行的服务0表示关
  • 安装vnc的各种悲剧解决

    系统 环境 VM 43 RHEL5 1 root 64 localhost vnc uname r 2 6 18 53 el5xen 本地XP系统安装 VNCVIEW去控制VM中的RHEL5 1 下面在LINUX上安装VNCSERVER 1
  • iOS基础 UITabBarController

    使用 创建子控制器继承自UITabBarController xff0c 在viewDidLoad阶段 xff0c 把各个分页上的控制器给创建好 xff0c 用UITabBarController的方法addChildControoler相
  • 插入内核模块失败提示"Invalid module format"

    产品需要编译自己的定制内核 43 内核模块 xff0c 下载内核源码定制修改后rpmbuild方式 点击打开链接 编译升级内核 xff0c 如下方式编译内核模块 make C kernel source SUBDIRS 61 96 pwd
  • microsoft visual c++ build tools

    因为visual studio的安装包太大 xff0c 所以在不需要开发的情况下 xff0c 可以选择使用microsoft visual c 43 43 build tools安装c 43 43 编译器 xff0c 这个工具会小很多 安装
  • C++ 应用程序 内存结构 --- BSS段,数据段,代码段,堆内存和栈

    转自 xff1a http hi baidu com C6 BF D6 D0 B5 C4 C5 AE CE D7 blog item 5043d08e741075f3503d922c html ld 时把所有的目标文件的代码段组合成一个代码
  • 4.1 简单题 - B 恭喜你

    当别人告诉你自己考了 x 分的时候 xff0c 你要回答说 xff1a 恭喜你考了 x 分 xff01 比如小明告诉你他考了90分 xff0c 你就用汉语拼音打出来 gong xi ni kao le 90 fen 输入格式 xff1a 输
  • <script>在页面代码上没有显示

    记录一下 导入js文件 xff0c 自己路径都没有问题 xff0c 为什么在浏览器查看页面代码没有自己写的那行js导入文件的代码呢 xff0c 原来 xff0c 是之前看着不舒服 xff0c 点了exclude xff0c exclude是
  • 利用Rust构建一个REST API服务

    利用Rust构建一个REST API服务 关注公众号 xff1a 香菜粉丝 了解更多精彩内容 Rust 是一个拥有很多忠实粉丝的编程语言 xff0c 还是很难找到一些用它构建的项目 xff0c 而且掌握起来甚至有点难度 想要开始学习一门编程
  • 安装cmake3.22

    升级cmake版本 脚本 span class token assign left variable file name span span class token operator 61 span cmake 3 22 0 yum era
  • stdout stderr 重定向到文件

    1 stdout stderr 重定向 1 stdout stderr 重定向 1 1 dup dup2 重定向到已打开文件 或 新文件1 2 freopen 重定向到新文件1 3 命令行重定向1 4 参考资料 1 1 dup dup2 重
  • 逆向基础-Windows驱动开发(一)

    Windows内核开发 第一个驱动程序 环境配置 xff1a 安装WDK xff1a WDK版本与SDK保持一致 然后记得把Spectre Mitigation给Disabled掉 xff0c 就不用去下载漏洞补丁了 然后在内核层 xff0
  • json-c 理解记录

    1 json c 理解记录 1 json c 理解记录 1 1 编译及说明1 2 特色1 3 使用 1 3 1 创建 xff0c 读写文件1 3 2 拷贝1 3 3 增改 1 3 3 1 字典增加元素1 3 3 2 数组增加修改元素 1 3