cJSON 解析

https://github.com/DaveGamble/cJSON

https://blog.csdn.net/bjbz_cxy/article/details/79173603

1. 介紹

/* 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;

2. 使用

2.1. 基礎函數

  • cJSON_Parse
/* Supply a block of JSON, and this returns a cJSON object you can interrogate. */
CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value);

作用:將一個JSON數據包载绿,按照cJSON結構體的結構序列化,并在堆中開辟一塊內存存儲cJSON結構體
返回值:成功返回一個指向內存塊中的cJSON的指針,失敗返回NULL-

  • cJSON_GetObjectItem
/* Get item "string" from object. Case insensitive. */
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string);

作用:獲取JSON字符串字段值
返回值:成功返回一個指向cJSON類型的結構體指針,失敗返回NULL

  • cJSON_GetArraySize
/* Returns the number of items in an array (or object). */
CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array);

作用:獲取數組成員對象個數
返回值:數組成員對象個數

  • cJSON_Print
/* Render a cJSON entity to text for transfer/storage. */
CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item);

作用:將cJSON數據解析成JSON字符串,并在堆中開辟一塊char的內存空間存儲JSON字符串
返回值:成功返回一個char
指針該指針指向位于堆中JSON字符串浪听,失敗返回NULL

  • cJSON_Delete
/* Delete a cJSON entity and all subentities. */
CJSON_PUBLIC(void) cJSON_Delete(cJSON *c);

作用:釋放位于堆中cJSON結構體內存
返回值:無

2.2. 簡單使用

#include <stdio.h>
#include "cJSON.h"
?
?
int main()
{
 char* json_string = \
 // 用char* 模擬一個JSON字符串
 "{"\
 "test_1": "0"\
 "test_2": "1"\
 "test_3": "2"\
 "}";

 // JSON字符串到cJSON格式
 cJSON* root = cJSON_Parse(json_string);
 // 判斷是否解析成功
 if (!root)
 {
 printf("json pack into cjson error...");
 }
 else
 {
 cJSON_Print(root);
 }
?
 // 獲取字段值
 char* test_1_string = cJSON_GetObjectltem(root, "test_1")->valuestring;
 char* test_2_string = cJSON_GetObjectltem(root, "test_2")->valuestring;
 char* test_3_string = cJSON_GetObjectltem(root, "test_3")->valuestring;
?
 // 打印輸出
 printf("%s", *test_1_string);
 printf("%s", *test_2_string);
 printf("%s", *test_3_string);
?
 // delete cjson
 cJSON_Delete(root);
}

2.3. 解析數組

#include <stdio.h>
#include "cJSON.h"
?
?
int main()
{
 char* json_string = \
 // 用char* 模擬一個JSON字符串
 "{ ", \
 "test_1":"1", \
 "test_2":"2", \
 "test_arr":"[{", \
 "{", \
 "test_1":"arr_1", \
 "test_2":"arr_2", \
 "test_3":"arr_3", \
 "}", \
 "{"\
 "test_1":"1", \
 "test_2":"2", \
 "test_3":"3", \
 "}", \
 "}]", \
 "test_3" :"3", \
 "}"http://json_arr string;
?
 // 解析JSON字符串
 cJSON* root = cJSON_Parse(json_string);
?
 // 判斷是否解析成功
 if (!root)
 {
 printf("cjson error…")
 }
 else
 {
 cJSON_Print(root);
 }
?
 // 獲取數組對象
 cJSON* test_arr = cJSON_GetObjectItem(root, "test_arr");
?
 // 獲取數組對象個數
 int arr_size = cJSON_GetArraySize(test_arr);
?
 // 獲取test_arr數組對象孩子節(jié)點
 cJSON* arr_item = test_arr->child;
?
 // 循環(huán)獲取數組下每個字段的值并使用cJSON_Print打印
 for (int i = 0; i < arr_size; ++i)
 {
 cJSON_Print(cJSON_GetObjectItem(arr_item, "test_1"));
 cJSON_Print(cJSON_GetObjectItem(arr_item, "test_2"));
 cJSON_Print(cJSON_GetObjectItem(arr_item, "test_3"));
 arr_item = arr_item->next;//下一個子對象
 }
?
 // delete cjson
 cJSON_Delete(root);
}
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末院溺,一起剝皮案震驚了整個濱河市窒所,隨后出現的幾起案子鹉勒,更是在濱河造成了極大的恐慌,老刑警劉巖吵取,帶你破解...
    沈念sama閱讀 218,546評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件禽额,死亡現場離奇詭異,居然都是意外死亡皮官,警方通過查閱死者的電腦和手機脯倒,發(fā)現死者居然都...
    沈念sama閱讀 93,224評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來捺氢,“玉大人藻丢,你說我怎么就攤上這事∩闫梗” “怎么了悠反?”我有些...
    開封第一講書人閱讀 164,911評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長馍佑。 經常有香客問我问慎,道長,這世上最難降的妖魔是什么挤茄? 我笑而不...
    開封第一講書人閱讀 58,737評論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮冰木,結果婚禮上穷劈,老公的妹妹穿的比我還像新娘。我一直安慰自己踊沸,他們只是感情好歇终,可當我...
    茶點故事閱讀 67,753評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著逼龟,像睡著了一般评凝。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上腺律,一...
    開封第一講書人閱讀 51,598評論 1 305
  • 那天奕短,我揣著相機與錄音,去河邊找鬼匀钧。 笑死翎碑,一個胖子當著我的面吹牛,可吹牛的內容都是我干的之斯。 我是一名探鬼主播日杈,決...
    沈念sama閱讀 40,338評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了莉擒?” 一聲冷哼從身側響起酿炸,我...
    開封第一講書人閱讀 39,249評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎涨冀,沒想到半個月后填硕,有當地人在樹林里發(fā)現了一具尸體,經...
    沈念sama閱讀 45,696評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡蝇裤,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,888評論 3 336
  • 正文 我和宋清朗相戀三年廷支,在試婚紗的時候發(fā)現自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片栓辜。...
    茶點故事閱讀 40,013評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡恋拍,死狀恐怖,靈堂內的尸體忽然破棺而出藕甩,到底是詐尸還是另有隱情施敢,我是刑警寧澤园蝠,帶...
    沈念sama閱讀 35,731評論 5 346
  • 正文 年R本政府宣布付呕,位于F島的核電站,受9級特大地震影響嗜价,放射性物質發(fā)生泄漏腋妙。R本人自食惡果不足惜默怨,卻給世界環(huán)境...
    茶點故事閱讀 41,348評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望骤素。 院中可真熱鬧匙睹,春花似錦、人聲如沸济竹。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,929評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽送浊。三九已至梦谜,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間袭景,已是汗流浹背唁桩。 一陣腳步聲響...
    開封第一講書人閱讀 33,048評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留浴讯,地道東北人朵夏。 一個月前我還...
    沈念sama閱讀 48,203評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像榆纽,于是被迫代替她去往敵國和親仰猖。 傳聞我的和親對象是個殘疾皇子捏肢,可洞房花燭夜當晚...
    茶點故事閱讀 44,960評論 2 355

推薦閱讀更多精彩內容