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;

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

scott198512

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值