租用服務(wù)器建設(shè)網(wǎng)站費(fèi)用win10優(yōu)化大師有用嗎
C/C++ 中 JSON 庫的使用 (CJSON/nlohmann)
- 概述
- cjson
- 基本操作
- 從(字符指針)緩沖區(qū)中解析出JSON結(jié)構(gòu)
- 轉(zhuǎn)成成JS字符串(將傳入的JSON結(jié)構(gòu)轉(zhuǎn)化為字符串)
- 將JSON結(jié)構(gòu)所占用的數(shù)據(jù)空間釋放
- JSON 值的創(chuàng)建
- 創(chuàng)建一個(gè)值類型的數(shù)據(jù)
- 創(chuàng)建一個(gè)對(duì)象(文檔)
- 數(shù)組創(chuàng)建以及添加
- JSON嵌套
- 向?qū)ο笾性黾渔I值對(duì)
- 向數(shù)組中增加對(duì)象
- 幾個(gè)能提高操作效率的宏函數(shù)
- 查找JSON值
- 根據(jù)鍵找json結(jié)點(diǎn)
- 判斷是否有key是string的項(xiàng)
- 返回?cái)?shù)組結(jié)點(diǎn)array中成員的個(gè)數(shù)
- 根據(jù)數(shù)組下標(biāo)index取array數(shù)組結(jié)點(diǎn)的第index個(gè)成員
- 遍歷數(shù)組
- nlohmann
- nlohmann庫使用代碼示例
概述
純C環(huán)境中使用
cjson
庫,C++環(huán)境中也可以使用nlohmann
庫,本文介紹基本的使用場景,如需更詳細(xì)的介紹可以查看庫官方文檔。
nlohmann:
nlohmann庫(https://github.com/nlohmann/json)提供了豐富而且符合直覺的接口(https://json.nlohmann.me/api/basic_json/),只需導(dǎo)入頭文件即可使用,方便整合到項(xiàng)目中。
CJSON:
JSON: JavaScript Object Notation(JavaScript 對(duì)象表示法),是輕量級(jí)的存儲(chǔ)和交換文本信息的語法,類似 XML . 特點(diǎn)是純文本(純字符串)、層級(jí)結(jié)構(gòu)、使用數(shù)組。
cJson:一個(gè)基于 C 語言的 Json 庫,它是一個(gè)開源項(xiàng)目,github 下載地址:https://github.com/DaveGamble/cJSON
cJson庫組成:主要的文件有兩個(gè),一個(gè) cJSON.c 一個(gè) cJSON.h。使用時(shí),將頭文件 include 進(jìn)去即可
cjson
typedef struct cJSON { //cJSON結(jié)構(gòu)體struct cJSON*next,*prev; /* 遍歷數(shù)組或?qū)ο箧湹那跋蚧蚝笙蜴湵碇羔?/struct cJSON *child; /*數(shù)組或?qū)ο蟮暮⒆庸?jié)點(diǎn)*/int type; /* key的類型*/char *valuestring; /*字符串值*/int valueint; /* 整數(shù)值*/double valuedouble; /* 浮點(diǎn)數(shù)值*/char *string; /* key的名字*/
} cJSON_st;
extern cJSON *cJSON_Parse(const char *value); //解析一塊JSON數(shù)據(jù)返回cJSON結(jié)構(gòu), 在使用完之后調(diào)用cJSON_Delete函數(shù)釋放json對(duì)象結(jié)構(gòu)。
解析JSON數(shù)據(jù)包,并按照cJSON結(jié)構(gòu)體的結(jié)構(gòu)序列化整個(gè)數(shù)據(jù)包。
使用該函數(shù)會(huì)通過malloc函數(shù)在內(nèi)存中開辟一個(gè)空間,使用完成需要手動(dòng)釋放。
extern char *cJSON_Print(cJSON *item); //可用于輸出到輸出設(shè)備, 使用完之后free(char *) cJSON_PrintUnformatted(cJSON *item); //類似,沒有格式,即轉(zhuǎn)換出的字符串中間不會(huì)有"\n" "\t"之類的東西存在.
void cJSON_Delete(cJSON *c) //會(huì)將其下的所有節(jié)點(diǎn)的資源一并釋放掉!!
-
JSON 值的創(chuàng)建
-
創(chuàng)建一個(gè)值類型的數(shù)據(jù)
extern cJSON *cJSON_CreateNumber(double num);//創(chuàng)建 extern cJSON *cJSON_CreateString(const char *string); //創(chuàng)建 extern cJSON *cJSON_CreateArray(void); //創(chuàng)建json數(shù)組
extern cJSON *cJSON_CreateObject(void); //創(chuàng)建一個(gè)根數(shù)據(jù)項(xiàng),之后便可向該根數(shù)據(jù)項(xiàng)中添加string或int等內(nèi)容
cJSON *cJSON_CreateIntArray(const int *numbers,int count);void cJSON_AddItemToArray(cJSON *array, cJSON *item);
-
JSON嵌套
-
向?qū)ο笾性黾渔I值對(duì)
cJSON_AddItemToObject(root, "rows", 值類型數(shù)據(jù)相關(guān)函數(shù)());
- 向?qū)ο笾性黾訑?shù)組
cJSON_AddItemToObject(root, "rows", cJSON_CreateArray()); //創(chuàng)建一個(gè)根數(shù)據(jù)項(xiàng),之后便可向該根數(shù)據(jù)項(xiàng)中添加string或int等內(nèi)容
cJSON_AddItemToArray(rows, cJSON_CreateObject()); //向json數(shù)組中增加元素/對(duì)象
#define cJSON_AddNumberToObject(object,name,n) cJSON_AddItemToObject(object, name,cJSON_CreateNumber(n))
//向json對(duì)象中添加數(shù)字,節(jié)點(diǎn)名and節(jié)點(diǎn)值#define cJSON_AddStringToObject(object,name,s) cJSON_AddItemToObject(object, name, cJSON_CreateString(s))
// 向json對(duì)象中添加字符串
-
查找JSON值
-
根據(jù)鍵找json結(jié)點(diǎn)
extern cJSON *cJSON_GetObjectItem(cJSON *object,const char*string) //從cJSON結(jié)構(gòu)體中查找某個(gè)子節(jié)點(diǎn)名稱(鍵名稱),如果查找成功可把該子節(jié)點(diǎn)序列化到cJSON結(jié)構(gòu)體中。
extern int cJSON_HasObjectItem(cJSON *object,const char *string){ return cJSON_GetObjectItem(object,string)?1:0; } //如果有返回1 否則返回0
extern int cJSON_GetArraySize(cJSON *array);
extern cJSON *cJSON_GetArrayItem(cJSON *array,int index); //返回該成員節(jié)點(diǎn)
#define cJSON_ArrayForEach(pos, head) for(pos = (head)->child; pos != NULL; pos = pos->next) cJSON_ReplaceItemInObject(json,"data",cJSON_CreateString("hello")) //用于代替json對(duì)象中data元組的值
nlohmann
使用方式比較簡單,下面直接通過示例來說明.
nlohmann庫使用代碼示例
#pragma once#include <iostream> #include <fstream> #include <string> #include <nlohmann/json.hpp>static bool JsonConfigInit(const std::string& config_file)bool ret = false;std::ifstream cfg_file(config_file);if (!cfg_file.is_open()) {nlohmann::json configjson; //創(chuàng)建一個(gè)空結(jié)構(gòu)configjson["config_settings"] = { {"bool_value", true},{"file_size", 15},{"info_file", "./tmp.ini"}};//對(duì)對(duì)象進(jìn)行初始化std::ofstream(config_file.c_str()) << configjson;std::cout << "create config json file"<< std::endl;return false;}try{std::cout << "JsonConfigInit from \" " << config_file<< "\" "<< std::endl;const nlohmann::json& file_json = nlohmann::json::parse(cfg_file);//判斷首節(jié)點(diǎn)是否存在if(file_json.is_null() || (file_json.contains("config_settings") == false ) || file_json.at("config_settings").size() == 0){ret = false; goto json_end;}nlohmann::json set_json = file_json.at("config_settings"); // 獲取相應(yīng)的鍵值對(duì),get_to必須保證雙方類型一致,否則極易出現(xiàn)段錯(cuò)誤bool m_json_bool;int m_json_number;std::string m_json_string ;if(set_json.at("bool_value").is_boolean()){set_json.at("bool_value").get_to(m_json_bool);}if(set_json.at("file_size").is_number()){set_json.at("file_size").get_to(m_json_number);}if(set_json.at("info_file").is_string()){set_json.at("info_file").get_to(m_json_string);}ret = true;}}catch (nlohmann::json::parse_error& ex){ std::cerr << "JsonConfigInit failed:parse error ! ! reason: [" << ex.what() << "]"<< std::endl;}catch (nlohmann::json::exception& ex){std::cerr << "JsonConfigInit parse_json parse fail! reason: [" << ex.what() << "]"<< std::endl;}json_end: cfg_file.close();return ret;}