当前位置:网站首页>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、常用的函数在网上都能找到
创建 和 取值
边栏推荐
- sqli-labs(less-8)
- OpenAtom OpenHarmony分论坛圆满举办,生态与产业发展迈向新征程
- How to build knowledge management system in enterprises and institutions
- Arduino Pro Mini atmega328p connect and light the first LED (at the same time, record the problem of burning failure stk500_recv)
- MSP430 开发中遇到的坑(待续)
- AsiaInfo technology released antdb7.0, a "Telecom grade" core transaction database, to help government and enterprises "trust" to create the future!
- 太赞了!京东研发一哥力荐的高可用网站构建技术PDF,备好水,慢慢啃
- [dark horse morning post] LETV 400 employees have no 996 and no internal papers; Witness history! 1 euro =1 US dollar; Stop immediately when these two interfaces appear on wechat; The crackdown on cou
- Localization, low latency, green and low carbon: Alibaba cloud officially launched Fuzhou data center
- 新东方单季营收5.24亿美元同比降56.8% 学习中心减少925间
猜你喜欢

With the continuous waves of infringement, the U.S. patent and trademark office began to study the impact of NFT on copyright

HC-05蓝牙模块调试从模式和主模式经历

GMT installation and use

Developing NES games with C language (cc65) 10. Game cycle

Uninstall Navicat: genuine MySQL official client, really fragrant!

用C语言开发NES游戏(CC65)09、滚动

Using Arduino to develop esp8266 to build a development environment

洪九果品通过聆讯:5个月经营利润9亿 阿里与中国农垦是股东

Exploration on cache design optimization of community like business

腾讯二面:@Bean 与 @Component 用在同一个类上,会怎么样?
随机推荐
用C语言开发NES游戏(CC65)10、游戏循环
腾讯二面:@Bean 与 @Component 用在同一个类上,会怎么样?
Let Arduino support nuvotom Xintang
First in the country! The two standards of "data product registration" formulated by insight technology and Shandong data were officially released
[cute new problem solving] climb stairs
Newly released, the domestic ide developed by Alibaba is completely open source
Most of the interfaces of Tiktok are already available, and more interfaces are still open. Please look forward to it
leetcode:704二分查找
Kuzaobao: summary of Web3 encryption industry news on July 13
SQL injection less24 (secondary injection)
How to realize more multimedia functions through the ffmpeg library and NaPi mechanism integrated in openharmony system?
Library automatic reservation script
Fastjson parses multi-level JSON strings
用C语言开发NES游戏(CC65)02、什么是v-blank?
Solve the PHP prompt warning: division by zero in error
Localization, low latency, green and low carbon: Alibaba cloud officially launched Fuzhou data center
洪九果品通过聆讯:5个月经营利润9亿 阿里与中国农垦是股东
Some API interfaces purchased by Yiwu hope to bring you some help
Markdown concise grammar manual
[try to hack] intranet Foundation