C++解析Json的方法詳解【jsoncpp】
本文實例講述了C++解析Json的方法。分享給大家供大家參考,具體如下:
JSON(JavaScript Object Notation) 是一種輕量級的數(shù)據(jù)交換格式,和xml類似,本文主要對VS2008中使用Jsoncpp解析json的方法做一下記錄。
Jsoncpp是個跨平臺的開源庫,下載地址:http://sourceforge.net/projects/jsoncpp/,我下載的是v0.5.0,壓縮包大約104K。
方法一:使用Jsoncpp生成的lib文件
解壓上面下載的Jsoncpp文件,在jsoncpp-src-0.5.0/makefiles/vs71目錄里找到j(luò)soncpp.sln,用VS2008版本編譯,默認生成靜態(tài)鏈接庫。 在工程中引用,只需要包含include/json下的頭文件及生成的.lib文件即可。
如何包含lib文件:在.cpp文件中#pragma comment(lib."json_vc71_libmt.lib"),在工程屬性中Linker下Input中Additional Dependencies寫入lib文件名字(Release下為json_vc71_libmt.lib,Debug為json_vc71_libmtd.lib)
注意:Jsoncpp的lib工程編譯選項要和VS工程中的編譯選項保持一致。如lib文件工程編譯選項為MT(或MTd),VS工程中也要選擇MT(或MTd),否則會出現(xiàn)編譯錯誤問題,debug和release下生成的lib文件名字不同,注意不要看錯了,當(dāng)成一個文件來使用(我就犯了這個錯誤)。
方法二:使用Jsoncpp包中的.cpp和.h文件
解壓上面下載的Jsoncpp文件,把jsoncpp-src-0.5.0文件拷貝到工程目錄下,將jsoncpp-src-0.5.0\jsoncpp-src-0.5.0\include\json和jsoncpp-src-0.5.0\jsoncpp-src-0.5.0\src\lib_json目錄里的文件包含到VS工程中,在VS工程的屬性C/C++下General中Additional Include Directories包含頭文件目錄.\jsoncpp-src-0.5.0\include。在使用的cpp文件中包含json頭文件即可,如:#include "json/json.h"。將json_reader.cpp、json_value.cpp和json_writer.cpp三個文件的Precompiled Header屬性設(shè)置為Not Using Precompiled Headers,否則編譯會出現(xiàn)錯誤。
jsoncpp 使用詳解
jsoncpp 主要包含三種類型的 class:Value、Reader、Writer。jsoncpp 中所有對象、類名都在 namespace Json 中,包含 json.h 即可。
Json::Value 只能處理 ANSI 類型的字符串,如果 C++ 程序是用 Unicode 編碼的,最好加一個 Adapt 類來適配。
下面是從網(wǎng)上找的代碼示例:
1. 從字符串解析json
const char* str = "{\"uploadid\": \"UP000000\",\"code\": 100,\"msg\": \"\",\"files\": \"\"}";
Json::Reader reader;
Json::Value root;
if (reader.parse(str, root)) // reader將Json字符串解析到root,root將包含Json里所有子元素
{
std::string upload_id = root["uploadid"].asString(); // 訪問節(jié)點,upload_id = "UP000000"
int code = root["code"].asInt(); // 訪問節(jié)點,code = 100
}
2. 從文件解析json
int ReadJsonFromFile(const char* filename)
{
Json::Reader reader;// 解析json用Json::Reader
Json::Value root; // Json::Value是一種很重要的類型,可以代表任意類型。如int, string, object, array
std::ifstream is;
is.open (filename, std::ios::binary );
if (reader.parse(is, root, FALSE))
{
std::string code;
if (!root["files"].isNull()) // 訪問節(jié)點,Access an object value by name, create a null member if it does not exist.
code = root["uploadid"].asString();
code = root.get("uploadid", "null").asString();// 訪問節(jié)點,Return the member named key if it exist, defaultValue otherwise.
int file_size = root["files"].size(); // 得到"files"的數(shù)組個數(shù)
for(int i = 0; i < file_size; ++i) // 遍歷數(shù)組
{
Json::Value val_image = root["files"][i]["images"];
int image_size = val_image.size();
for(int j = 0; j < image_size; ++j)
{
std::string type = val_image[j]["type"].asString();
std::string url = val_image[j]["url"].asString();
printf("type : %s, url : %s \n", type.c_str(), url.c_str());
}
}
}
is.close();
return 0;
}
3. 向文件中插入json
void WriteJsonData(const char* filename)
{
Json::Reader reader;
Json::Value root; // Json::Value是一種很重要的類型,可以代表任意類型。如int, string, object, array
std::ifstream is;
is.open (filename, std::ios::binary );
if (reader.parse(is, root))
{
Json::Value arrayObj; // 構(gòu)建對象
Json::Value new_item, new_item1;
new_item["date"] = "2011-11-11";
new_item1["time"] = "11:11:11";
arrayObj.append(new_item); // 插入數(shù)組成員
arrayObj.append(new_item1); // 插入數(shù)組成員
int file_size = root["files"].size();
for(int i = 0; i < file_size; ++i)
root["files"][i]["exifs"] = arrayObj; // 插入原json中
std::string out = root.toStyledString();
// 輸出無格式j(luò)son字符串
Json::FastWriter writer;
std::string strWrite = writer.write(root);
std::ofstream ofs;
ofs.open("test_write.json");
ofs << strWrite;
ofs.close();
}
is.close();
}
備注:Json試用不當(dāng)會導(dǎo)致程序崩潰
Json::Value root; Json::Reader reader;
最好作為main函數(shù)的變量,不要作為全局變量,不要多次聲明(即,不要在循環(huán)或者在其他函數(shù)中聲明)。因為其static屬性,在第一次使用結(jié)束后會被析構(gòu),后來的使用就會訪問無效地址。
json_value.cpp中
Value::~Value() {
switch (type_) {
case nullValue:
case intValue:
case uintValue:
case realValue:
case booleanValue:
break;
case stringValue:
if (allocated_)
releaseStringValue(value_.string_);
break;
case arrayValue:
case objectValue:
delete value_.map_;//!!!!!!
break;
default:
JSON_ASSERT_UNREACHABLE;
}
正確的使用方式如下:
int getRebalancing(string str, Json::Value root, Json::Reader reader) ;
int main() {
Json::Value root;
Json::Reader reader;
while(1){
getRebalancing(string::str, root, reader);
//do something
}
return 0;
}
PS:這里再為大家推薦幾款比較實用的json在線工具供大家參考使用:
在線JSON代碼檢驗、檢驗、美化、格式化工具:
http://tools.jb51.net/code/json
JSON在線格式化工具:
http://tools.jb51.net/code/jsonformat
在線XML/JSON互相轉(zhuǎn)換工具:
http://tools.jb51.net/code/xmljson
json代碼在線格式化/美化/壓縮/編輯/轉(zhuǎn)換工具:
http://tools.jb51.net/code/jsoncodeformat
C語言風(fēng)格/HTML/CSS/json代碼格式化美化工具:
http://tools.jb51.net/code/ccode_html_css_json
希望本文所述對大家C++程序設(shè)計有所幫助。
相關(guān)文章
C語言中關(guān)于動態(tài)內(nèi)存分配的詳解
動態(tài)內(nèi)存是指在堆上分配的內(nèi)存,而靜態(tài)內(nèi)存是指在棧上分配的內(nèi)存。棧上分配的內(nèi)存是由系統(tǒng)分配和釋放的,空間有限,在復(fù)合語句或函數(shù)運行結(jié)束后就會被系統(tǒng)自動釋放而堆上分配的內(nèi)存則不會有這個問題。2021-09-09
C語言修煉之路悟徹數(shù)組真妙理?巧用下標(biāo)破萬敵上篇
在C語言和C++等語言中,數(shù)組元素全為指針變量的數(shù)組稱為指針數(shù)組,指針數(shù)組中的元素都必須具有相同的存儲類型、指向相同數(shù)據(jù)類型的指針變量。指針數(shù)組比較適合用來指向若干個字符串,使字符串處理更加方便、靈活2022-02-02
c/c++拷貝構(gòu)造函數(shù)和關(guān)鍵字explicit詳解
這篇文章主要介紹了c/c++拷貝構(gòu)造函數(shù)和關(guān)鍵字explicit的相關(guān)知識,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-08-08
C語言深入刨析數(shù)據(jù)結(jié)構(gòu)之棧與鏈棧的設(shè)計與應(yīng)用
棧是限定僅在表尾進行插入或刪除操作的線性表,表尾稱為棧頂(top),表頭稱為棧底(bottom)。棧的最主要特點就是“先進后出”(FILO),或“后進先出”(LIFO)。用鏈?zhǔn)酱鎯Y(jié)構(gòu)表示的棧稱為“鏈棧”,鏈棧對應(yīng)于鏈表2022-05-05
C++關(guān)鍵字volatile學(xué)習(xí)筆記
這篇文章主要為大家介紹了C++關(guān)鍵字volatile學(xué)習(xí)筆記,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10

