C語言中cJSON的使用
一、CJSON初識
JSON (JavaScript Object Notation)是一種輕量級的數(shù)據(jù)交換格式,常用于在網(wǎng)絡之間傳輸數(shù)據(jù)。它是一種文本格式,易于人閱讀和編寫,同時也易于機器解析和生成。JSON基于JavaScript語言的一部分,但已經(jīng)成為獨立于編程語言的通用數(shù)據(jù)格式。
以下是JSON的幾個常見用途:
1.數(shù)據(jù)交換: JSON是一種廣泛使用的數(shù)據(jù)交換格式,它可以在客戶端和服務器之間傳輸各種類型的數(shù)據(jù)結構,例如配置信息、日志、用戶數(shù)據(jù)等。
2.Web應用程序: JSON作為JavaScript的一種數(shù)據(jù)格式,廣泛應用于Web應用程序的前端設計。JavaScript的Ajax請求通常會使用JSON來與后端進行通信。
3.NoSQL數(shù)據(jù)庫: NoSQL數(shù)據(jù)庫,如MongoDB、CouchDB等,通常使用JSON格式存儲數(shù)據(jù)。這些數(shù)據(jù)庫支持JSON數(shù)據(jù)類型,使得存儲和查詢JSON數(shù)據(jù)非常方便。
4.配置文件:JSON格式非常易于閱讀和編寫,因此可以用作應用程序的配置文件。由于JSON具有層級因此可以將復雜配置文件分解為易于管理的部分。
5.移動應用程序: JSON用于移動應用程序通常涉及與Web API的交互,讀取和保存本地數(shù)據(jù)和共享數(shù)據(jù)
JSON在Web開發(fā)中得到廣泛應用,特別是在前后端數(shù)據(jù)交互方面。許多APl(Application Programming Interface)以SON格式返回數(shù)據(jù),因為它易于解析和生成,同時具有良好的可讀性。前端框架(如React、Angular、vue)通常使用)SON格式來處理數(shù)據(jù).
JSON的簡潔性和通用性使其成為一種理想的數(shù)據(jù)交換格式,不僅在Web開發(fā)中使用廣泛,而且在各種應用程序和領域中都有著重要的作用。
源碼下載:
github:https://github.com/DaveGamble/cJSON/releases
gitee:https://gitee.com/mirrors/cJSON
二、CJSON解析器基礎
1.對象(Object):用花括號表示,包含一組無序的鍵值對。每個鍵值對之間用逗號分隔。
{ "key1":"value1", "key2":"value2", "key3":"value3" }
⒉.數(shù)組(Array):用方括號[表示,包含一組有序的值。每個值之間用逗號分隔。
{ "arraykey":[1,2,3,4] }
3.值(value):可以是字符串、數(shù)字、布爾值、對象、數(shù)組、null等。
{ "stringKey": "Hello, JSON", "numberKey": 42, "booleanKey": true, "nullKey": null }
4.字符串(String):由雙引號括起來的Unicode字符序列。
{ "name": "John Doe", "city" : "New York" }
5.數(shù)字(Number) :可以是整數(shù)或浮點數(shù)。
{ "integer": 42, "flaot": 3.14 }
6.布爾值(Boolean) :表示真或假。
{ "isTrue": true, "isFalse": false }
7.null:表示空值
{ "emptyValue": null }
//基本流程與相關API //1.創(chuàng)建cJsoN指針 cJSON json=NULL; //2.解析整段JsON數(shù)據(jù),返回cJsoN指針 (cJSON*) cJSON_Parse (const char *value) ; //3.根據(jù)鍵值對的名稱從鏈表中取出對應的值,返回該鍵值對的地址 (cJSON*) cJsON_GetObjectItem(const cJSON * const object,const char * const string); //4.根據(jù)類型提取相關值 //5.釋放 void cJSON_Delete(cJSON* item) ;
cJSON結構:
type:
三、CJSON解析數(shù)據(jù)
JSON解析基礎
int main() { const char* json_data = {"{\"name:lihua\",\"age\":18}"}; cJSON* json = NULL; json = cJSON_Parse(json_data); if(json == NULL) { printf("error:%s",cJSON_ErrorPtr()); } else { cJSON* json_name = cJSON_GetObjectItem(json,"name"); //if(json_name->type == cJSON_String) //{ // printf("%s\n",json_name->valuestring); //} if(cJSON_IsString(json_name)) { printf("%s\n",json_name->valuestring); } cJSON* json_age = cJSON_GetObjectItem(json,"age"); //if(json_name->type == cJSON_Number) //{ // printf("%d\n",json_name->valueint); //} if(cJSON_IsNumber(json_age)) { printf("%d\n",json_name->valueint); } cJSON_Delete(json); } return 0; }
CJSON解析數(shù)組數(shù)據(jù)
cJSON解析數(shù)組:
//基本流程與相關API //1.創(chuàng)建cJsON指針 cJSON json=NULL; //2.解析整段JsON數(shù)據(jù),返回cJsoN指針 (cJSON*) cJSON_Parse (const char *value) ; //3.根據(jù)鍵值對的名稱從鏈表中取出對應的值,返回該鍵值對的地址 (cJSON *) cJSON_GetObjectItem(const cJSON * const object,const char * const string); //4.讀取數(shù)組 //先獲取大小 (int) cJSON_GetArraySize(const cJSON* array); //再序號讀取 (cJSON*) cJSON_GetArrayItem(const cJSON* array,int index); //5.釋放 void cJSON_Delete(cJSON* item);
案例:
void testArray() { const char* json_data = "[1,2,3,4,5,6,]"; cJSON* root = cJSON_Parse(json_data); if(root == NULL) { printf("%s\n",cJSON_ErrorPtr()); return; } if(cJSON_IsArray(root)) { int array_size = cJSON_GetArraySize(root); for(int i = 0;i < array_size;i++) { cJSON* item = cJSON_GetArrayItem(root,i); if(cJSON_IsNumber(item)) { int value = item->valueint; printf("%d ",value); } printf("%\n"); } } cJSON_Delete(root); } // 讀取帶鍵的二維數(shù)組 void testArray2D() { const char* json_data = "{\"array\":[[1,2,3],[4,5,6],[7,8,9]]}"; cJSON* root = cJSON_Parse(json_data); cJSON* array = cJSON_GetObjectItem(root,"array"); int rows = cJSON_GetArraySize(array); for(int i = 0;i < rows;i++) { cJSON* rowJSON = cJSON_GetArrayItem(array,i); int cols = cJSON_GetArraySize(rowJSON); for(int j = 0;j < cols;j++) { int value = cJSON_GetArrayItem(array,j)->valueint; printf("%d ",value); } printf("\n"); } cJSON_Delete(root); } int main() { return 0; }
CJSON解析嵌套數(shù)據(jù)
// 讀取想要讀取的數(shù)據(jù) void test_by_oneself() { const char* json_data = "{\"name\":\"lihua\",\"age\":18,\"address\":{\"city\":\"chongqing\",\"phone\":[\"123456\",\"78910\"]}}"; cJSON* root = cJSON_Parse(json_data); if(root == NULL) { printf("%s\n",cJSON_ErrorPtr()); return; } cJSON* address = cJSON_GetObjectItem(root,"address"); cJSON* city = cJSON_GetObjectItem(address,"city"); if(cJSON_IsString(city)) { printf("%s\n",city->valuestring); } cJSON_Delete(root); }
遞歸的方式:
void read_json(cJSON* root) { if(root == NULL) { return; } switch(root->type) { case cJSON_Object: { cJSON* child = root->child; if(child) { read_json(child); child = child->next; } } break; case cJSON_Array: { int array_size = cJSON_GetArraySize(root); for(int i = 0;i < array_size;i++) { cJSON* item = cJSON_GetArrayItem(root,i); printf("%s\n",root->string); read_json(item); } } break; case cJSON_String: printf("%s : %s\n",root->string,root->valuestring); break; case cJSON_Number: if(root->valueint - root->valuedouble == 0) { printf("%s : %d\n",root->string,root->valueint); } else { printf("%s : %lf",root->string,root->valuedouble); } break; case cJSON_True: printf("true\n"); break; case cJSON_False: printf("false\n"); break; case cJSON_NULL: printf("NULL\n"); break; default: break; } } // 遞歸方式 void test_read_json() { const char* json_data = "{\"name\":\"lihua\",\"age\":18,\"address\":{\"city\":\"chongqing\",\"phone\":[\"123456\",\"78910\"]}}"; cJSON* root = cJSON_Parse(json_data); if(root == NULL) { printf("%s\n",cJSON_ErrorPtr()); return; } read_json(root); cJSON_Delete(root); }
四、創(chuàng)建JSON數(shù)據(jù)
創(chuàng)建空的JSON對象:
cJSON* cJSON_CreateObject();
添加基本內(nèi)容:
cJSON_AddNumberToObject(cJSON * const object, const char * const name, const double number) cJSON_AddStringToObject(cJSON * const object, const char * const name, const double number)
添加嵌套:
cJSON_AddItemToObject(cJSON *object, const char *string, cJSON*item)
添加數(shù)組:
cJSON_AddItemToArray(cJSON *array, cJSON *item)
打印JSON:
cJSON_Print(const cJSON*item)
案例:
#include <stdio.h> #include <stdlib.h> #include <time.h> #include "cJSON.h" int main() { srand(time(NULL)); cJSON* root = cJSON_CreateObject(); // 添加基本內(nèi)容 cJSON_AddStringToObject(root,"name","lihua"); cJSON_AddNumberToObject(root,"age",18); // 添加嵌套 cJSON* address = cJSON_CreateObject(); cJson_AddStringToObject(address,"city","chongqing"); cJSON_AddItemToObject(root,"address",address); // 添加數(shù)組 cJSON* score = cJSON_CreateArray(); for(int i = 0;i < 5;i++) { cJSON_AddItemToArray(score,cJSON_CreateNumber(rand() % 10 + 90)); } cJSON_AddItemToObject(root,"score",score); printf("%s\n",cJSON_Print(root)); cJSON_Delete(root); return 0; }
到此這篇關于C語言中cJSON的使用的文章就介紹到這了,更多相關C語言 cJSON內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
OnSize、OnSizing和OnGetMinMaxInfo區(qū)別分析
這篇文章主要介紹了OnSize、OnSizing和OnGetMinMaxInfo區(qū)別分析,需要的朋友可以參考下2015-01-01c語言鏈表基本操作(帶有創(chuàng)建鏈表 刪除 打印 插入)
這篇文章主要介紹了c語言鏈表基本操作,大家參考使用吧2013-12-12