当前位置: 首页 > news >正文

json-c 理解记录

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

相关文章:

  • 实践分享!GitLab CI/CD 快速入门
  • LabVIEW中的数据通信方法
  • 排序链表,
  • ESP32-arduino,超好玩的定时器!
  • Python selenium 页面滚动
  • 【FPGA教程案例69】硬件开发板调试9——通过ila在线调试DDS,并通过HDMI接口在显示器上显示正弦波形
  • MeterSphere专题之: 配套的浏览器插件:chrome-extensions
  • 【FPGA教程案例70】硬件开发板调试10——vivado程序固化详细操作步骤
  • 计算机毕业设计ssm青年志愿者社团管理36uiu系统+程序+源码+lw+远程部署
  • 数据结构————树
  • 【操作系统】 第二章 —— 系统调用 中断 异常
  • 移动端测试
  • Cmake、Qt与VS编译VTK(生成QVTK)
  • Java——JDBC(Java DataBase Connectivity)数据库连接技术
  • Express
  • 【vuex入门系列02】mutation接收单个参数和多个参数
  • 08.Android之View事件问题
  • Angular 4.x 动态创建组件
  • C++11: atomic 头文件
  • ERLANG 网工修炼笔记 ---- UDP
  • es的写入过程
  • Git初体验
  • IDEA常用插件整理
  • 干货 | 以太坊Mist负责人教你建立无服务器应用
  • 规范化安全开发 KOA 手脚架
  • 极限编程 (Extreme Programming) - 发布计划 (Release Planning)
  • 简单基于spring的redis配置(单机和集群模式)
  • 力扣(LeetCode)22
  • 前端性能优化——回流与重绘
  • 想写好前端,先练好内功
  • 湖北分布式智能数据采集方法有哪些?
  • #《AI中文版》V3 第 1 章 概述
  • #NOIP 2014# day.1 生活大爆炸版 石头剪刀布
  • #我与Java虚拟机的故事#连载06:收获颇多的经典之作
  • $L^p$ 调和函数恒为零
  • (7)STL算法之交换赋值
  • (cos^2 X)的定积分,求积分 ∫sin^2(x) dx
  • (TOJ2804)Even? Odd?
  • (二十四)Flask之flask-session组件
  • (附源码)spring boot基于小程序酒店疫情系统 毕业设计 091931
  • (附源码)ssm基于web技术的医务志愿者管理系统 毕业设计 100910
  • (蓝桥杯每日一题)平方末尾及补充(常用的字符串函数功能)
  • (亲测成功)在centos7.5上安装kvm,通过VNC远程连接并创建多台ubuntu虚拟机(ubuntu server版本)...
  • (转)scrum常见工具列表
  • (转)项目管理杂谈-我所期望的新人
  • (转)一些感悟
  • .bat批处理(十一):替换字符串中包含百分号%的子串
  • .NET C# 使用 SetWindowsHookEx 监听鼠标或键盘消息以及此方法的坑
  • .Net MVC4 上传大文件,并保存表单
  • @RequestBody详解:用于获取请求体中的Json格式参数
  • @Transactional类内部访问失效原因详解
  • [AIGC] Spring Interceptor 拦截器详解
  • [Android学习笔记]ScrollView的使用
  • [AR]Vumark(下一代条形码)
  • [BUUCTF NewStarCTF 2023 公开赛道] week4 crypto/pwn