1. 程式人生 > >使用jsoncpp解析JSON資料

使用jsoncpp解析JSON資料

上節《JSON資料格式》中我們講到了JSON的資料格式和一些應用的地方,接著上面的說下去,jsoncpp和boost.property_tree都可以解析JSON檔案,我們先將jsoncpp如何解析。

1.jsoncpp是什麼?
2. 編譯jsoncpp

jsoncpp檔案中提供了vs71的工程檔案以及makerelease.py檔案,用來編譯,裡面分為jsontest、lib_json、test_lib_json三個工程,按照自己需要的編譯。

注意:如果使用VS預設的編譯選項MTd或者MT,在使用json_libmtd.lib的時候可能會出現LNK2038錯誤(我使用的VS2012 vc110環境)所以請修改MTD為MDd,MT為MD

3.使用jsoncpp讀JSON檔案

如何將lib庫新增進VS工程中在此就不贅述了。先看第一個讀檔案的例

// JSON檔案
{"address":[
    {"name":"eliteYang", "email":"[email protected]"},
    {"name":"AAA", "email":"[email protected]"},
    {"name":"BBB", "email":"[email protected]"}
]}
/**
 * file     :   jsoncpp_test.cpp
 * author   :   eliteYang
 * email    :   
[email protected]
* blog : http://www.cppfasn.org * desc : json cpp test */ #include <fstream> #include <string> #include "jsoncpp/json.h" int _tmain(int argc, _TCHAR* argv[]) { std::ifstream ifs; ifs.open("test.json"); Json::Reader reader; Json::Value root; if (!reader.parse(ifs, root, false)) { return -1; } Json::Value add_value = root["address"]; for (int i = 0; i < add_value.size(); ++i) { Json::Value temp_value = add_value[i]; std::string strName = temp_value["name"].asString(); std::string strMail = temp_value["email"].asString(); std::cout << "name: " << strName << " email: " << strMail << std::endl; // use value array[index] //Json::Value temp_value = add_value[i]; //std::string strName = add_value[i]["name"].asString(); //std::string strMail = add_value[i]["email"].asString(); //std::cout << "name: " << strName << " email: " << strMail << std::endl; } system("Pause"); return 0; }

結果:

name: eliteYang email: [email protected]
name: AAA email: [email protected]
name: BBB email: [email protected]
請按任意鍵繼續. . .

跟我們檔案中的資料完全一致。

4.使用JSON寫入一塊資料

我們繼續使用上述檔案,在中間加上一塊資料。我們插入一個新的{"name": "append", "email": "[email protected]"}

/**
 * file     :   jsoncpp_test.cpp
 * author   :   eliteYang
 * email    :   [email protected]
 * blog     :   http://www.cppfasn.org
 * desc     :   json cpp test
 */
 
#include <fstream>
#include <string>
#include "jsoncpp/json.h"
 
int _tmain(int argc, _TCHAR* argv[])
{
    std::ifstream ifs;
    ifs.open("test.json");
 
    Json::Reader reader;
    Json::Value root;
    if (!reader.parse(ifs, root, false))
    { return -1; }
 
    Json::Value& add_value = root["address"];
    Json::Value append_value;
    append_value["name"] = "append";
    append_value["email"] = "[email protected]";
    add_value.append(append_value);
 
    for (int i = 0; i < add_value.size(); ++i)
    {
        Json::Value temp_value = add_value[i];
        std::string strName = temp_value["name"].asString();
        std::string strMail = temp_value["email"].asString();
        std::cout << "name: " << strName << " email: " << strMail << std::endl;
    }
 
    Json::FastWriter writer;
    std::string json_append_file = writer.write(root);
 
    std::ofstream ofs;
    ofs.open("test_append.json");
    ofs << json_append_file;
 
    system("Pause");
 
    return 0;
}

結果:

name: eliteYang email: [email protected]
name: AAA email: [email protected]
name: BBB email: [email protected]
name: append email: [email protected]
請按任意鍵繼續. . .
// test_append.json
{"address":[{"email":"[email protected]","name":"eliteYang"},{"email":"[email protected]","name":"AAA"},{"email":"[email protected]","name":"BBB"},{"email":"[email protected]","name":"append"}]}

當然了,jsoncpp對陣列的解析也支援STL中迭代器的風格,不過我個人覺得還是陣列好理解一些。迭代器的解析風格就不寫了,大家可以自己摸索下,主要是使用Json::Value::Members。

JSON官方還是非常推薦用jsoncpp來解析JSON檔案的,大家也看到了,確實比較方便。

btw.因為這兩天筆記本散熱不好,所以接了套螺絲刀將筆記本大卸八塊,掃了掃灰,然後又裝上了,結果神奇的就好了,果然是賤啊,欠拆。所以json系列的文章就晚了幾天,非常抱歉,好了,不早了,該睡覺了,各位晚安。