cJSON簡介
cJSON是使用C語言編寫,用來創(chuàng)建、解析JSON文件的庫。cJSON特點就是工程文件簡單,只有一個.c
和一個.h
,但提供函數(shù)接口功能齊全,麻雀雖小五臟俱全,使得在嵌入式工程中使用起來得心應手。
cJSON獲取
https://github.com/DaveGamble/cJSON
只需拉取cJSON.c
和cJSON.h
即可。
cJSON解析
給出如下JSON格式示例
{
"messageType": 2,
"messageId": "16493268950279230864908057508987",
"imei": "864908057508987898607B1192180035422",
"iccid": "898607B1192180035422",
"reportTime": "2022-12-16 15:49:00",
"gpsData":
[
{
"Longitude":31.23453232,
"Latitude":118.45345431
},
{
"Longitude":31.23453245,
"Latitude":118.45345443
}
{
"Longitude":31.23453255,
"Latitude":118.45345453
}
]
}
1、先創(chuàng)建根節(jié)點指針
cJSON *root = NULL;
root = cJSON_Parse(msg);
cJSON其實是個結(jié)構體類型,被定義如下,cJSON_Parse
接口拿到我們的msg
數(shù)據(jù)后會創(chuàng)建一個cJSON結(jié)構實例
,我們創(chuàng)建的cJSON結(jié)構體指針
root,會指向這個cJSON結(jié)構體實例
。既然是實例,就會申請內(nèi)存。所以最后我們一定要釋放掉這塊內(nèi)存,否則將導致內(nèi)存泄漏
/* 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;
cJSON結(jié)構體其實是一個抽象的鍵值對。
他通過成員:前節(jié)點、后節(jié)點、子節(jié)點,來實現(xiàn)JSON結(jié)構的嵌套,JSON結(jié)構最終在內(nèi)存中是以一個鏈表的形式存在。
struct cJSON *next; //前節(jié)點
struct cJSON *prev; //后節(jié)點
struct cJSON *child;//子節(jié)點
而每個節(jié)點通過以下成員存儲鍵值
char *valuestring;
int valueint;
double valuedouble;
2、獲取鍵值對
是int類型鍵值就指向valueint
,是字符串類型指向valuestring
,字符串數(shù)據(jù)我們可以用strcpy拷貝出來
cJSON *msgType = NULL;
msgType = cJSON_GetObjectItem(root , "messageType" );
int type = msgType ->valueint
char id[21];
cJSON *iccid = NULL;
iccid = cJSON_GetObjectItem(root , "iccid" );
strcpy(id, iccid ->valuestring);
3、獲取數(shù)組
如果出現(xiàn)像示例中gpsData這種JSON數(shù)組,數(shù)組下面又嵌套著新的JSON數(shù)據(jù)的,我們將使用以下API
cJSON* gpsDataArray = NULL;
cJSON* gpsPoint = NULL;
int gpsArraySize = 0;
double Longitude = 0//經(jīng)度
double Latitude = 0 //緯度
gpsDataArray = cJSON_GetObjectItem(root, "gpsData");
gpsArraySize = cJSON_GetArraySize(gpsDataArray);
for(i = 0; i < gpsArraySize ; i++)
{
gpsPoint = cJSON_GetArrayItem(gpsDataArray , i);
Longitude = cJSON_GetObjectItem(gpsPoint , "Longitude");
Latitude = cJSON_GetObjectItem(gpsPoint , "Latitude");
}
4、釋放內(nèi)存 !
cJSON_Delete(root);
cJSON組裝
現(xiàn)在我們試圖組裝《cJSON解析》
中的示例
1、同樣,創(chuàng)建根節(jié)點
cJSON* root = NULL;
root = cJSON_CreateObject();
2、添加鍵值對成員
cJSON_AddNumberToObject(root , "messageType", 2);
cJSON_AddStringToObject(root , "messageId", "16493268950279230864908057508987");
cJSON_AddStringToObject(root , "imei", "864908057508987898607B1192180035422");
cJSON_AddStringToObject(root , "iccid", "898607B1192180035422");
cJSON_AddStringToObject(root , "reportTime","2022-12-16 15:49:00");
3、添加數(shù)組
和解析相反,我們應該先創(chuàng)建gps點,然后把點添加到數(shù)組,再把數(shù)組添加到根節(jié)點
//先創(chuàng)建3個gps點
cJSON* gpsData1 = NULL;
gpsData2 = cJSON_CreateObject();
cJSON_AddNumberToObject(gpsData1 ,"Longitude", 31.23453232);
cJSON_AddNumberToObject(gpsData1 ,"Latitude", 118.45345431;
cJSON* gpsData2 = NULL;
gpsData2 = cJSON_CreateObject();
cJSON_AddNumberToObject(gpsData2 ,"Longitude", 31.23453245);
cJSON_AddNumberToObject(gpsData2 ,"Latitude", 118.45345443;
cJSON* gpsData3 = NULL;
gpsData3 = cJSON_CreateObject();
cJSON_AddNumberToObject(gpsData3 ,"Longitude", 31.23453255);
cJSON_AddNumberToObject(gpsData3 ,"Latitude", 118.45345453;
//創(chuàng)建數(shù)組,把gps點添加到gps數(shù)組
cJSON* gpsArray = NULL;
gpsArray = cJSON_CreateArray();
cJSON_AddItemToArray(gpsArray ,gpsData1 );
cJSON_AddItemToArray(gpsArray ,gpsData2 );
cJSON_AddItemToArray(gpsArray ,gpsData3 );
//把gps數(shù)組添加到根節(jié)點
cJSON_AddItemToObject(root,"gpsData",gpsArray );
4、輸出組裝的JSON文章來源:http://www.zghlxwxcb.cn/news/detail-425425.html
char *msg;
msg = cJSON_Print(root);
5、依然要記得釋放內(nèi)存文章來源地址http://www.zghlxwxcb.cn/news/detail-425425.html
cJSON_Delete(root);
到了這里,關于【嵌入式開源庫:cJSON】 一個輕量級C語言JSON數(shù)據(jù)解析庫用法詳解的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!