当前位置:网站首页>解决方法:STM32使用cJSON解析数据失败
解决方法:STM32使用cJSON解析数据失败
2022-06-30 15:50:00 【Leung_ManWah】
一、问题
在 STM32 移植 cJSON 库后,使用 cJSON_Parse(),解析失败。
char cmd[512] = "{\"msg\":\"this is successful start up\",\"result\":1,\"action\":\"req_startUp\",\"responseData\":\"trustedDevice\",\"serial_no\":\"0341\",\"timestamp\":1656489886238}";
cJSON *pRoot = cJSON_Parse(cmd);
const char *error_ptr = cJSON_GetErrorPtr();
if (error_ptr != NULL)
{
printf("Error before: %s\n", error_ptr);
}
使用 cJSON_GetErrorPtr() 分析出以下错误:
Error before: ,"timestamp":1656489886238}
而当去掉 serial_no 字段后,则能够解析成功。
char cmd[512] = "{\"msg\":\"this is successful start up\",\"result\":1,\"action\":\"req_startUp\",\"responseData\":\"trustedDevice\",\"timestamp\":1656489886238}";
二、原因
当解析的的数据比较长时,会解析失败,但是短的数据则没有问题,后面排查是因为 cJSON 解析需要用到的内存比较大,溢出导致解析失败。
查看 STM32 启动文件。如 startup_stm32f407xx.s,发现:
- 栈区(stack):由编译器自动分配和释放,存放函数的参数值、局部变量的值等,其操作方式类似于数据结构中的栈。
- 堆区(heap):一般由程序员分配和释放,若程序员不释放,程序结束时可能由操作系统回收。分配方式类似于数据结构中的链表。
**注意:**记得要 cJSON_Delete 删除对象,还有 cJSON_Print 会一直不停申请内存,所有调用完之后一定要 free 释放,否则多次调用后一样会内存溢出。导致后面的 cJSON_Print 会申请不到内存。
三、解决方法
增大空间后,问题解决
• 由 Leung 写于 2022 年 6 月 30 日
边栏推荐
- 力士乐液控单向阀Z2S10-1-3X/
- addmodule_allmerge_ams_im
- 编译丨迅为iTOP4412开发板Makefile编译
- Construction schedule of intelligent management and control platform in Shanxi Chemical Industry Park
- 异常类_日志框架
- 9: Chapter 3: e-commerce engineering analysis: 4: [general module]; (to be written...)
- A scheduled task deletes data at a specified time
- Cmakelists Basics
- TCP Socket与TCP 连接
- 定时任务删除指定时间的的数据
猜你喜欢

“推广+搞笑剧情”,如何碰撞出爆款的火花?

Cesium-1.72 learning (earth rotation)

Compile u-boot source code for stm32p157 development board

differential analysis between different groups nichenet for silicosis成功运行!

IO stream_ recursion

Compile - compile for itop4412 development board makefile
![Delete duplicates in an ordered array ii[double pointers -- unified in multiple cases]](/img/e2/cadfdbe476a86cb2d72c1ae0160a4a.png)
Delete duplicates in an ordered array ii[double pointers -- unified in multiple cases]

"Promotion + funny plot", how to collide with the sparks of popular funds?

Mathematical modeling for war preparation 36 time series model 2

Exercise book of introduction to database system
随机推荐
simpleITK读取nii遇到ITK only supports orthonormal direction cosines的错误
MySQL advanced notes
阿里云盘分享压缩包
美国穆格moog伺服阀D661-4577C
CGR 21 (D,E,F)
Etcd tutorial - Chapter 9 etcd implementation of distributed locks
期未课程设计:基于SSM的产品销售管理系统
Hologres shared cluster helps Taobao subscribe to the extreme refined operation
STL教程7-set、pair对组和仿函数
Drug management system plus database, overnight, plus report
山西化工园区智能化管控平台建设时间表
利用PIL进行不失真的resize
Eight basic sorting (detailed explanation)
Several cross end development artifacts
名单揭晓 | 2021年度中国杰出知识产权服务团队
Nodejs learning notes II
Etcd教程 — 第九章 Etcd之实现分布式锁
Required plug-ins for idea
nichenet实战silicosis
Lambda expression_ Stream stream_ File class