cjson解析器说明
1. cJSON
cJson 是c语言编写的一个解析器. 是一个超轻巧,携带方便,单文件,简单的可以作为ANSI-C标准的JSON解析器。主要两个文件cJSON.c 和cJSON.h . 主要用来编码和解析数据.
2. JSON格式
语法:键 / 值
1、以 "{"开始,以 "}" 结束,允许嵌套使用
2、每个键和值成对出现,并使用:分隔。如"age"=23
3、键值对之间用 ,分隔
值的多种类型:
字符串:用 " "
{"name":"code","gender":"male"
}
数字:整数或浮点数都直接表示
{"age":20,"value":50.0
}
数组:用[ ]
{"key1" : [0, 1],"key2" : [2, 3]}
布尔值:
false,true
3.CJSON数据结构定义
#ifndef cJSON__h
#define cJSON__h#ifdef __cplusplus
extern "C"
{
#endif/* project version */
#define CJSON_VERSION_MAJOR 1
#define CJSON_VERSION_MINOR 4
#define CJSON_VERSION_PATCH 6#include <stddef.h>/* cJSON Types: */
#define cJSON_Invalid (0)
#define cJSON_False (1 << 0)
#define cJSON_True (1 << 1)
#define cJSON_NULL (1 << 2)
#define cJSON_Number (1 << 3)
#define cJSON_String (1 << 4)
#define cJSON_Array (1 << 5)
#define cJSON_Object (1 << 6)
#define cJSON_Raw (1 << 7) /* raw json */#define cJSON_IsReference 256
#define cJSON_StringIsConst 512/* The cJSON structure: */
typedef struct cJSON
{/* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */struct cJSON *next;struct cJSON *prev;/* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */struct cJSON *child;/* The type of the item, as above. */int type;/* The item's string, if type==cJSON_String and type == cJSON_Raw */char *valuestring;/* writing to valueint is DEPRECATED, use cJSON_SetNumberValue instead */int valueint;/* The item's number, if type==cJSON_Number */double valuedouble;/* The item's name string, if this item is the child of, or is in the list of subitems of an object. */char *string;
} cJSON;
4. 解析
解析过程按解析过程来描述一次:
(1) 首先调用cJSON_Parse()函数,解析JSON数据包,并按照cJSON结构体的结构序列化整个数据包。使用该函数会通过malloc()函数在内存中开辟一个空间,使用完成需要手动释放。
cJSON*root=cJSON_Parse(json_string);
(2) 调用cJSON_GetObjectItem()函数,可从cJSON结构体中查找某个子节点名称(键名称),如果查找成功可把该子节点序列化到cJSON结构体中。
cJSON*item=cJSON_GetObjectItem(root,"firstName");
(3) 如果需要使用cJSON结构体中的内容,可通过cJSON结构体中的valueint和valuestring取出有价值的内容(即键的值)
本例子中,我们直接访问 item->valuestring 就获取到 "Brett" 的内容了。
(4) 通过cJSON_Delete(),释放cJSON_Parse()分配出来的内存空间。
cJSON_Delete(root);
1.解析数值
cJSON* cjson_head = NULL;cJSON* cjson_info = NULL;cjson_head = cJSON_GetObjectItem(message, "head");cjson_info = cJSON_GetObjectItem(message, "info");timeStamp = cJSON_GetObjectItem(cjson_head, "timeStamp")->valuedouble;msgId = cJSON_GetObjectItem(cjson_head, "msgId")->valueint;
2.解析字符串
cjson_info = cJSON_GetObjectItem(message, "info");
Position = cJSON_GetObjectItem(cjson_info, "Position")->valuestring;
3.解析数组
cjson_ObjectArray = cJSON_GetObjectItem(cjson_info, "ObjectArray");/* 解析数组 */ObjectArray_size = cJSON_GetArraySize(cjson_ObjectArray);for (i = 0; i < ObjectArray_size; i++){cjson_ObjectArray_item = cJSON_GetArrayItem(cjson_ObjectArray, i);CrashRisk = cJSON_GetObjectItem(cjson_ObjectArray_item, "CrashRisk")->valueint;ID = cJSON_GetObjectItem(cjson_ObjectArray_item, "ID")->valueint;ObjectHeading = cJSON_GetObjectItem(cjson_ObjectArray_item, "ObjectHeading")->valuedouble;ObjectSubtype = cJSON_GetObjectItem(cjson_ObjectArray_item, "ObjectSubtype")->valueint;ObjectType = cJSON_GetObjectItem(cjson_ObjectArray_item, "ObjectType")->valueint;Position = cJSON_GetObjectItem(cjson_ObjectArray_item, "Position")->valuestring;RelativeVelocity = cJSON_GetObjectItem(cjson_ObjectArray_item, "Velocity")->valuestring;Shape = cJSON_GetObjectItem(cjson_ObjectArray_item, "Shape")->valuestring;VehicleBrakeLightTypeEnum = cJSON_GetObjectItem(cjson_ObjectArray_item, "VehicleBrakeLight")->valueint;VehicleBlinkLightEnum = cJSON_GetObjectItem(cjson_ObjectArray_item, "VehicleBlinkLightEnum")->valueint;
4.解析long 类型
(long)(test->valuedouble)
double timeStamp = 0.0;
timeStamp = cJSON_GetObjectItem(cjson_head, "timeStamp")->valuedouble;
long ts = (long long) timeStamp;
cjson解析器说明相关推荐
- cJSON_译(C中的超轻量级JSON解析器)
cJSON ANSI C中的超轻量级JSON解析器. 通行证 执照 版权所有(c)2009-2017 Dave Gamble和cJSON贡献者 特此免费授予获得此软件和相关文档文件("软件& ...
- cjson 对象是json数组型结构体_C语言 - cJSON解析特定格式 含有数组array类型的数据...
在ESP32中使用了cJSON库,发现很好用.最近服务器端的JSON格式越来越多样,还是有些注意点,需要做下笔记记录下来. cJSON *MAC_arry = cJSON_GetObjectItem( ...
- DIY 一个 JSON解析器。
从最早使用的C-JSON,性能没什么问题,缺点是最大只支持32层嵌套,不过可以通过修改宏来增加嵌套层数, 最近使用LUA开发服务器业务部分,找了几个JSON库,要么标准支持不尽人意,要么则是使用LUA ...
- X、elua解析器开源文档:合集
前言 嵌入式开发人员一般情况下都是从C语言开始入手的,然而C语言的学习难度较大,入门门槛也比较高.很多人因为自身C语言的瓶颈导致很难做出来复杂的产品.有的人也仅仅是因为兴趣爱好只是想diy做些小玩 ...
- LeetCode简单题之设计 Goal 解析器
题目 请你设计一个可以解释字符串 command 的 Goal 解析器 .command 由 "G"."()" 和/或 "(al)" 按某种 ...
- CSS 选择器:BeautifulSoup4解析器
和 lxml 一样,Beautiful Soup 也是一个HTML/XML的解析器,主要的功能也是如何解析和提取 HTML/XML 数据. lxml 只会局部遍历,而Beautiful Soup 是基 ...
- php codeigniter 语言,php – codeigniter模板引擎,包括语言解析器
不幸的是,CI内置的模板解析器类没有此功能.你可以在 sparks directory中环顾四周,有多个火花集成了许多模板引擎,如smarty或twig,可以通过调整来创建这样的东西. 此外,您可以尝 ...
- rest-framework之解析器
rest-framework之解析器 本文目录 一 解析器的作用 二 全局使用解析器 三 局部使用解析器 四 源码分析 回到目录 一 解析器的作用 根据请求头 content-type 选择对应的解析 ...
- 【C++】clipp 一个命令行参数解析器
1.简介 clipp是一个使用方便.功能强大的命令行解析器,源码只有一个头文件<clipp.h> github地址:https://github.com/muellan/clipp 2.使 ...
最新文章
- java 反射 getclass_java 反射getClass .class 的使用方法示例
- 32 六角oled驱动_Alienware下半年开售55英寸OLED显示器,4K120Hz、95%DCI-P3
- k8s:服务发现Service
- 【android】环形进度条实现
- python解析nginx配置文件_Nginx情景分析之配置文件解析
- 实现位数超过32bit的整数的加减乘除运算_Java 运算符
- as3通用三方库大全
- centos是什么系统_解决更换操作系统后的开机问题
- 使用lambdaquery() 报空指针异常_ARMv8 异常处理简介
- react-native-router-flux 页面跳转与传值
- 美女程序员在国外:余露 – Android开发者在新加坡
- 学堂云 意在象中-中国古典诗词鉴赏
- 2022华为机试真题 C++ 实现【勾股数元组】
- CSS卡贴悬停展开效果
- NCRE考试感想 四级嵌入式(上)
- 小心肝队-冲刺日志(第六天)
- 北京工作居住证与北京居住证的区别
- 韶音骨传导耳机值得入手吗?南卡和韶音骨传导耳机实测对比
- libtool 的使用。
- 2018云南-华为软件产业峰会成功举办 加快构建数字中国