当前位置:网站首页>C语言项目中使用json
C语言项目中使用json
2022-07-28 11:40:00 【yi_tianchou】
随着物联网的发展,越来越多的物需要连接网络上传数据,服务器同时需要对接多种类型的终端,协议的多样性是加大服务器端复杂度的一个重要方面,同时也加大了维护复杂度,经过权衡终端最终采用了json数据格式。
使用的理由:
1、标准 主流的语言都是支持json,方便 终端和服务器的解析
2、扩展性好
3、易于维护
缺点:
比二进制的数据类型占用的带宽要大
使用方法:
1、下载
https://sourceforge.net/projects/cjson/
官方项目中有测试包 及使用教程
2、将cjson包 包含到项目中 #include "cJSON.h"
3、注意cjson 源文件中的 malloc 和free方法的替换
void cJSON_InitHooks(cJSON_Hooks* hooks)
{
if (!hooks) { /* Reset hooks */
cJSON_malloc = vm_malloc;
cJSON_free = vm_free;
return;
}
cJSON_malloc = (hooks->malloc_fn)?hooks->malloc_fn:vm_malloc;
cJSON_free = (hooks->free_fn)?hooks->free_fn:vm_free;
}
我使用的是 vm_malloc 和 vm_free 所以替换成我自己的方法了
4、json转字符串后,在字符串使用完之后要释放 因为char* 是使用malloc创建的内存空间
out = cJSON_Print(root); 或 out = cJSON_PrintUnformatted(root); 在out使用之后 要手动释放 否则会导致内存泄露
5、同理,字符串转json之后 在json使用完后要 调用 cJSON_Delete()释放json对象,因为json数是使用malloc创建的内存空间 如:
json = cJSON_Parse((char*)readBuf); 使用后要 cJSON_Delete(json );
6、常用的函数在网上都能找到
创建 和 取值
边栏推荐
- [try to hack] intranet Foundation
- 分布式定时器
- Open source office (ospo) unveils Secrets
- SQL injection less23 (filter comment)
- Developing NES games with C language (cc65) 04. Complete background
- Marketing play is changeable, and understanding the rules is the key!
- Yan Ji lost Beijing again, and more than half of the stores in the country were closed
- 用C语言开发NES游戏(CC65)06、精灵
- 卸载 Navicat:正版 MySQL 官方客户端,真香!
- Develop NES game (cc65) 07 and controller with C language (collision with spirit)
猜你喜欢

用C语言开发NES游戏(CC65)11、Metatiles

产学研用 共建开源人才生态 | 2022 开放原子全球开源峰会教育分论坛圆满召开

Ten prohibitions for men and women in love

揭秘界面控件DevExpress WinForms为何弃用受关注的MaskBox属性

用C语言开发NES游戏(CC65)10、游戏循环

论治理与创新 | 2022 开放原子全球开源峰会 OpenAnolis 分论坛圆满召开

Industry, University, research and application jointly build an open source talent ecosystem | the 2022 open atom global open source summit education sub forum was successfully held

Huawei releases harmonyos 3 and all scene new products, and the smart experience goes further

Open source office (ospo) unveils Secrets

【萌新解题】爬楼梯
随机推荐
西门子对接Leuze BPS_304i 笔记
Developing NES games with C language (cc65) 06. Sprites
Laravel form data validation
[half understood] zero value copy
20220728-Object类常用方法
Developing NES games with C language (cc65) 11. Metatiles
卸载 Navicat:正版 MySQL 官方客户端,真香!
Implementation method of mouse hover, click and double click in ue4/5
开源社区三十年 | 2022 开放原子全球开源峰会开源社区三十年专题活动圆满召开
How to build knowledge management system in enterprises and institutions
Redis实现分布式锁
First in the country! The two standards of "data product registration" formulated by insight technology and Shandong data were officially released
用C语言开发NES游戏(CC65)08、背景 碰撞
Huawei releases harmonyos 3 and all scene new products, and the smart experience goes further
PHP date time application: add or subtract the number of days of a specific date
【vulnhub】presidential1
HC-05蓝牙模块调试从模式和主模式经历
Analysys analysis: focus on users, improve the user experience of mobile banking, and help the growth of user value
Kuzaobao: summary of Web3 encryption industry news on July 13
Open source database innovation in the era of digital economy | the 2022 open atom global open source summit database sub forum was successfully held