擊左上方藍色“一口Linux”,選擇“設為星標”
第一時間看干貨文章 ?【干貨】嵌入式驅動工程師學習路線 ?【干貨】Linux嵌入式知識點-思維導圖-免費獲取 ?【就業】一個可以寫到簡歷的基于Linux物聯網綜合項目 ?【就業】簡歷模版

JSON:JavaScript對象表示法(JavaScript Object Notation)。是一種輕量級的數據交換格式。
它基于ECMAScript的一個子集。
JSON采用完全獨立于語言的文本格式,但是也使用了類似C語音家族的習慣(包括C、C++、C#、Java、JavaScript、Perl、Python等)。
這些特性使JSON成為理想的數據交換語言。
易于人閱讀和編寫,同時也易于機器解析和生成(一般用于提升網絡傳輸速率)。
JSON語法是JavaScript對象表示法語法的子集:
JSON的三種語法:
JSON的值可以是:
JSON結構有兩種:
舉例:
{
/*整型鍵值對*/
"student_num": 3,
/*數組*/
"stuinfo":[//數組開始
/*數組元素*/
{
"name": "彭于晏",
"no": "9527",
"sex": "男"
},
{
"name": "趙露思",
"no": "9528",
"sex": "女"
},
{
"name": "沈騰",
"sex": "male",
"no": "9529"
}
],//數組結尾
"rc": 0,
/*字符串鍵值對*/
"operation": "CALL",
"service": "telephone"
}
cJSON是一個僅有一個.h文件,一個.c文件組成的JSON解析器,它是由純C(ANSI C89)實現的,跨平臺性較好。cJSON是采用鏈表存儲的。
在GitHub上的地址為:
https://github.com/DaveGamble/cJSON
cJSON庫在使用的時候只需要如下兩步:
彭老師的測試代碼如下:
peng@ubuntu:~/work/test/cjson$ ls
cJSON.c cJSON.h main.c
main.c是彭老師編寫的測試程序, 編譯命令如下:
gcc cJSON.c main.c -o run -lm
/* 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。
/* Delete a cJSON entity and all subentities. */
CJSON_PUBLIC(void) cJSON_Delete(cJSON *c);
作用:釋放位于堆中cJSON結構體內存。
返回值:無
注意:在使用cJSON_Parse()獲取cJSON指針后,若不再使用了,則需要調用cJSON_Delete()對其釋放,否則會導致內存泄漏。
/* Render a cJSON entity to text for transfer/storage. */
CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item);
/* Render a cJSON entity to text for transfer/storage without any formatting. */
CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item);
作用:將cJSON數據解析成JSON字符串,并在堆中開辟一塊char*的內存空間存儲JSON字符串。cJSON_PrintUnformatted()與cJSON_Print()類似,只是打印輸出不帶格式,而只是一個字符串數據。
返回值:成功返回一個char*指針該指針指向位于堆中JSON字符串,失敗返回NULL。
注意:通過cJSON_Print()可以將cJSON鏈表中所有的cJSON對象打印出來,但是需要手動去釋放對應的資源:free(char *)。
/* returns the version of cJSON as a string */
CJSON_PUBLIC(const char*) cJSON_Version(void);
作用:獲取當前使用的cJSON庫的版本號。
返回值:返回一個字符串數據。
/* Returns the number of items in an array (or object). */
CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array);
作用:獲取數組cJSON對象數組元素個數。返回值:成功返回數組元素個數。
/* Retrieve item number "index" from array "array". Returns NULL if unsuccessful. */
CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index);
作用:從數組cJSON對象獲取下標為index的對向。返回值:成功返回一個指向內存塊中的cJSON的指針,失敗返回NULL。
/* Get item "string" from object. Case insensitive. */
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string);
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON * const object, const char * const string);
CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, const char *string);
/* Check if the item is a string and return its valuestring */
CJSON_PUBLIC(char *) cJSON_GetStringValue(cJSON *item);
下面的接口用于判斷具體cJSON指針指向的item類型:
/* These functions check the type of an item */
CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsFalse(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsTrue(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsBool(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsNull(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON * const item);
下面的接口用于創建指定類型的cJSON數據結構對象:
/* raw json */
CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(constchar *raw);
CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void);
CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void);
/* These calls create a cJSON item of the appropriate type. */
CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void);
CJSON_PUBLIC(cJSON *) cJSON_CreateTrue(void);
CJSON_PUBLIC(cJSON *) cJSON_CreateFalse(void);
CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool boolean);
CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num);
CJSON_PUBLIC(cJSON *) cJSON_CreateString(constchar *string);
/* These utilities create an Array of count items. */
CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(constint *numbers, int count);
CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(constfloat *numbers, int count);
CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(constdouble *numbers, int count);
CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(constchar **strings, int count);
下面的接口用于將創建的cJSON對象添加到鏈表中:
/* Append item to the specified array/object. */
CJSON_PUBLIC(void) cJSON_AddItemToArray(cJSON *array, cJSON *item);
CJSON_PUBLIC(void) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item);
下面的接口用于從現有的cJSON鏈表中刪除一個對象:
/* Remove/Detatch items from Arrays/Objects. */
CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const item);
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which);
CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which);
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string);
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, const char *string);
CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string);
CJSON_PUBLIC(void) cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string);
下面的接口用于創建并添加cJSON對象到指定的鏈表(簡化操作):
/* Helper functions for creating and adding items to an object at the same time.
* They return the added item or NULL on failure. */
CJSON_PUBLIC(cJSON*) cJSON_AddNullToObject(cJSON * const object, constchar * const name);
CJSON_PUBLIC(cJSON*) cJSON_AddTrueToObject(cJSON * const object, constchar * const name);
CJSON_PUBLIC(cJSON*) cJSON_AddFalseToObject(cJSON * const object, constchar * const name);
CJSON_PUBLIC(cJSON*) cJSON_AddBoolToObject(cJSON * const object, constchar * const name, const cJSON_bool boolean);
CJSON_PUBLIC(cJSON*) cJSON_AddNumberToObject(cJSON * const object, constchar * const name, constdouble number);
CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, constchar * const name, constchar * conststring);
CJSON_PUBLIC(cJSON*) cJSON_AddRawToObject(cJSON * const object, constchar * const name, constchar * const raw);
CJSON_PUBLIC(cJSON*) cJSON_AddObjectToObject(cJSON * const object, constchar * const name);
CJSON_PUBLIC(cJSON*) cJSON_AddArrayToObject(cJSON * const object, constchar * const name);
char * get_cjsonstr_from_file(char * filename)
{
FILE *fp = fopen(filename,"r");
int file_size = 0;
char * jsonStr = NULL;
if(fp== NULL){
cprintf(RED,"錯誤:無法打開文件%s\n",filename);
returnNULL;
}
//獲取文件大小
fseek(fp, 0,SEEK_END);
file_size = ftell(fp);
cprintf(YEL_H,"filelsize=%d\n",file_size);
fseek(fp, 0, SEEK_SET);// 讀取整個文件內容
jsonStr = (char*)malloc(file_size + 1);
if(jsonStr==NULL){
cprintf(RED,"錯誤:內存分配失敗\n");
fclose(fp);
returnNULL;
}
fread(jsonStr, 1, file_size, fp);
jsonStr[file_size] = '\0';
fclose(fp);
return jsonStr;
}
注意:該函數申請了內存,所以調用該函數,解析完畢,要把申請的內存free,
jsonStr = get_cjsonstr_from_file(CJSON_FILE_NAME);
if(jsonStr == NULL)
{
return -1;
}
myParse(jsonStr);
free(jsonStr);
下面是解析json字符串的所有鍵/值的內容:
打印結果如下:
我們嘗試修改json字符串
代碼與修改前后json字符串信息如下:
還可以將整個cjson對向內容打出來 有格式的方式打印Json:
cprintf(GREEN_H,"%s\n\n", cJSON_Print(root));
無格式方式打印json:
cprintf(GREEN_H,"%s\n\n", cJSON_PrintUnformatted(root));


后臺回復:cjson
end
一口Linux
關注,回復【1024】海量Linux資料贈送
精彩文章合集
文章推薦