一、JSON、cJSON簡介
1. JSON 簡介
JSON格式詳解
2. cJSON簡介
cJSON是一個使用C語言撰寫的JSON資料決議器,具有超輕便,可移植,單檔案的特點,使用MIT開源協議,其中主要包括兩個檔案cjson.c和cjson.h,
- cjson.h檔案中包含了對于JSON格式的結構體定義以及一些操作JSON格式的功能函式,包括創建JSON、向JSON格式中添加數字,字符,布林值等等、讀取JSON格式、將JSON格式轉化為字串等,
- cjson.c檔案中就是功能函式的具體實作,
cJSON原始碼下載地址
下載下來,解壓后,從里面找到兩個檔案(cJSON.c、cJSON.h),復制到我們的工程里面,只需在函式中包含頭檔案(#include “cJSON.h”),然后和cJSON.c一起編譯即可使用,
cJSON資料結構定義
具體代碼如下:
typedef struct cJSON {
struct cJSON *next,*prev; /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
struct cJSON *child; /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */
int type; /* The type of the item, as above. cjson結構的型別上面宏定義的其中之一*/
char *valuestring; /* The item's string, if type==cJSON_String */
int valueint; /* The item's number, if type==cJSON_Number */
double valuedouble; /* The item's number, if type==cJSON_Number */
char *string; /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
} cJSON;
二、使用cJSON構造JSON
1. cJSON庫函式介紹
介紹一些構造JSON時,經常用到的函式,
具體代碼如下:
CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void); //創建物件---常用
CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num); //創建數字
CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string); //創建字串
CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void); //創建陣列---常用
CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count); //創建整型陣列
CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count); //創建浮點型陣列---這個使用會出問題,需要浮點型可以使用雙浮點
CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count); //創建雙浮點型陣列
CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char *const *strings, int count); //創建字符型陣列
CJSON_PUBLIC(cJSON*) cJSON_AddNullToObject(cJSON * const object, const char * const name);//在物件中添加null
CJSON_PUBLIC(cJSON*) cJSON_AddTrueToObject(cJSON * const object, const char * const name);//在物件中添加true
CJSON_PUBLIC(cJSON*) cJSON_AddFalseToObject(cJSON * const object, const char * const name);//在物件中添加false
CJSON_PUBLIC(cJSON*) cJSON_AddNumberToObject(cJSON * const object, const char * const name, const double number); //在物件中添加數字
CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char * const name, const char * const string); //在物件中添加字串
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item); //在物件中添加專案
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToArray(cJSON *array, cJSON *item); //在陣列中添加專案---不常用
CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item); //JSON資料結構轉換為JSON字串---有格式
CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item); //JSON資料結構轉換為JSON字串---無格式
CJSON_PUBLIC(void) cJSON_Delete(cJSON *item); //清除結構體
2. 使用cJSON構造構造JSON
具體示例代碼如下:
#include <stdio.h>
#include "cJSON.h"
int main(void)
{
double grade[4]={66.5,118.5,61.5,128.5};
char *json_data = NULL ;
cJSON *TCP = cJSON_CreateObject(); //創建一個物件
cJSON_AddStringToObject(TCP,"name","MQ"); //添加字串
cJSON_AddNumberToObject(TCP,"age",25); //添加整型數字
cJSON_AddNumberToObject(TCP,"weight",66.66); // 添加浮點型數字
cJSON *ADD = cJSON_CreateObject();
cJSON_AddStringToObject(ADD,"country","China"); //添加字串
cJSON_AddNumberToObject(ADD,"zip-code",123456); //添加整型數字
cJSON_AddItemToObject(TCP,"address",ADD);
cJSON *SUB = cJSON_CreateArray();
cJSON_AddStringToObject(SUB,"","政治");
cJSON_AddStringToObject(SUB,"","數學");
cJSON_AddStringToObject(SUB,"","英語");
cJSON_AddStringToObject(SUB,"","專業課");
cJSON_AddItemToObject(TCP,"subject",SUB);
cJSON *GRA = cJSON_CreateDoubleArray(grade,4);
cJSON_AddItemToObject(TCP,"grade",GRA);
cJSON *STU = cJSON_CreateArray();
cJSON *Z3 = cJSON_CreateObject();
cJSON_AddStringToObject(Z3,"name","張三"); //添加字串
cJSON_AddNumberToObject(Z3,"age",24); //添加整型數字
cJSON_AddTrueToObject(Z3,"gender");
cJSON_AddItemToArray(STU,Z3);
cJSON *L4 = cJSON_CreateObject();
cJSON_AddStringToObject(L4,"name","李四"); //添加字串
cJSON_AddNumberToObject(L4,"age",25); //添加整型數字
cJSON_AddTrueToObject(L4,"gender");
cJSON_AddItemToArray(STU,L4);
cJSON *W5 = cJSON_CreateObject();
cJSON_AddStringToObject(W5,"name","王五"); //添加字串
cJSON_AddNumberToObject(W5,"age",26); //添加整型數字
cJSON_AddTrueToObject(W5,"gender");
cJSON_AddItemToArray(STU,W5);
cJSON_AddItemToObject(TCP,"student",STU);
cJSON_AddFalseToObject(TCP,"gender");
json_data = cJSON_Print(TCP); //JSON資料結構轉換為JSON字串
printf("%s\n",json_data);
cJSON_Delete(TCP);
return 0;
}
運行結果如下:
{
"name": "MQ", //字串
"age": 25, //整數
"weight": 66.66, //浮點數
"address":
{
"country": "China",
"zip-code": 123456
}, //物件
"subject": ["政治", "數學", "英語", "專業課"],//字符型陣列
"grade": [66.5, 118.5, 61.5, 128.5], //浮點型陣列
"student":[
{"name":"張三","age":24,"gender":ture},
{"name":"李四","age":25,"gender":ture},
{"name":"王五","age":26,"gender":ture},
], //物件型陣列
"gender": false //邏輯值
}
三、使用cJSON決議JSON
待更新,,,,,,,,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/237138.html
標籤:其他
上一篇:Javascript基礎:實作防抖函式(debounce)
下一篇:RFID——充值消費系統
