jsoncpp 使用詳解

2021-07-26 09:42:56 字數 1807 閱讀 8389

jsoncpp 主要包含三種型別的 class:value、reader、writer。jsoncpp 中所有物件、類名都在 namespace json 中,包含 json.h 即可。

json::value 只能處理 ansi 型別的字串,如果 c++ 程式是用 unicode 編碼的,最好加乙個 adapt 類來適配。

編譯問題:

error c1083: 無法開啟編譯器生成的檔案: 「../../build/vs71/release/lib_json\json_reader.asm」: no such file or directory 

解決方法:

修改靜態庫的工程屬性-->c++-->輸出檔案-->匯程式設計序輸出-->無列表。

#include

"include/json/json.h"

#include

#include

#include

using

namespace std;

int_tmain(int

argc, _tchar* argv)

";json::reader reader;

json::value root;

//從字串中讀取資料

if (reader.parse(str, root))

return 0;

// 從檔案中讀取

json::reader reader;

json::value root;

ifstream is;

is.open("personalinfo.json", ios::binary);

if (reader.parse(is, root))

cout << endl;

cout << "reading complete!"

<< endl;

// 寫入檔案

// 根節點 json::value root;

//根節點屬性

root["name"] = json::value("tsybius");

root["age"] = json::value(23);

root["***_is_male"] = json::value(true);

// 子節點

json::value partner;

//子節點屬性

partner["partner_name"] = json::value("galatea");

partner["partner_age"] = json::value(21);

partner["partner_***_is_male"] = json::value(false);

// 子節點掛到根節點上

root["partner"] = json::value(partner);

// 陣列形式

//直接輸出

cout << "fastwriter:"

<< endl;

json::fastwriter fw;

cout << fw.write(root) << endl << endl;

//縮排輸出

cout << "styledwriter:"

<< endl;

json::styledwriter sw;

cout << sw.write(root) << endl << endl;

//輸出到檔案

ofstream os;

os.open("personalinfo.json");

os << sw.write(root);

os.close();

JsonCpp使用優化

最近乙個專案在使用jsoncpp,jsoncpp簡潔易用的介面讓人印象深刻。但是在實際使用過程中,我發現jsoncpp的效能卻不盡如人意,所以想著方法優化下效能。理解 1 jsoncpp中一切都是value,value用union指向自己儲存的資料。value的型別分為兩種,一種是容器型別,比如ar...

jsoncpp使用示例

下面的示例程式顯示了jsoncpp的初步使用方法,包括了自定義物件的序列化等操作。include json json.h include using namespace std struct data可序列化物件 void deserialize json value root int main i...

JsonCpp使用簡介

json 名稱 值對 例如 firstname john json 物件在花括號中書寫,例如 json 陣列,陣列可包含多個物件 jsoncpp json value 是jsoncpp 中最基本 最重要的類,用於表示各種型別的物件,json reader 用來將記憶體或檔案中的json資料轉換成js...