從多個角度詳細闡述C++解析JSON

一、基本概念

1、JSON是一種輕量級數據交換格式。它被設計成易於人閱讀和編寫,並易於機器解析和生成。JSON是基於JavaScript語法的一個子集。

2、JSON中的數據類型包括:number, string, boolean, object, array, null。

3、C++解析JSON的庫很多,常用的有RapidJSON、boost.property_tree、nlohmann/json等等。

二、RapidJSON庫解析JSON

RapidJSON是C++的一種JSON解析庫,由於它的解析速度非常快,並且它提供了靈活的API,越來越多的人開始使用它。以下是一個使用RapidJSON解析JSON的例子:

#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include 
using namespace rapidjson;
using namespace std;
int main() {
    const char* json = "{\"name\":\"Jack\\\",\\\"age\\\":27}";
    Document document;
    document.Parse(json);
    Value& name = document[\"name\"];
    Value& age = document[\"age\"];
    cout << \"name:\" << name.GetString() << endl;
    cout << \"age:\" << age.GetInt() << endl;
    return 0;
}

三、boost.property_tree庫解析JSON

boost庫是C++程序員必備的一個庫,boost.property_tree庫是boost庫中的一個JSON解析庫。以下是一個使用boost.property_tree解析JSON的例子:

#include 
#include 
#include 
using namespace std;
using boost::property_tree::ptree;
int main() {
    std::string jsonStr = \"{ \\\"name\\\" : \\\"Jack\\\", \\\"age\\\" : 27 }\"; 
    stringstream ss(jsonStr);
    ptree pt;
    read_json(ss, pt);
    string name = pt.get(\"name\");
    int age = pt.get(\"age\");
    cout << \"name:\" << name << endl;
    cout << \"age:\" << age << endl;
    return 0;
}

四、nlohmann/json庫解析JSON

nlohmann/json是一個面向現代C++的JSON解析和生成庫。使用nlohmann/json解析JSON非常簡單,只需要包含一個頭文件即可。以下是一個使用nlohmann/json解析JSON的例子:

#include 
#include 
using namespace std;
using json = nlohmann::json;
int main() {
    string jstr = R\"({"name":"Jack","age":27})\"; 
    auto j = json::parse(jstr);
    string name = j[\"name\"];
    int age = j[\"age\"];
    cout << \"name:\" << name << endl;
    cout << \"age:\" << age << endl;
    return 0;
}

五、解析JSON中的數組

在JSON中,數組是一系列值,用方括號表示,值之間用逗號隔開。C++解析JSON中的數組需要使用循環進行操作,以下是一個使用nlohmann/json解析JSON中數組的例子:

#include 
#include 
using namespace std;
using json = nlohmann::json;
int main() {
    string jstr = R\"({\"data\":[{\"name\":\"Jack\",\"age\":27},{\"name\":\"Lucy\",\"age\":25}]})\"; 
    auto j = json::parse(jstr);
    auto data = j[\"data\"];
    for (auto& item : data) {
        string name = item[\"name\"];
        int age = item[\"age\"];
        cout << \"name:\" << name << \" age:\" << age << endl;
    }
    return 0;
}

六、異常處理

在解析JSON的過程中,可能會出現各種各樣的異常,例如JSON格式錯誤、缺少必要的鍵等等。為了避免程序出現異常或崩潰,我們需要對JSON解析器進行異常處理。以下是一個使用try-catch進行異常處理的例子:

#include 
#include 
#include 
using namespace std;
using json = nlohmann::json;
int main() {
    string jStr = R\"({\"name\":\"Jack\",\"age\":27})\"; 
    try {
        auto j = json::parse(jStr);
        string name = j.at(\"name\");
        int age = j.at(\"age\");
        cout << \"name:\" << name << \" age:\" << age << endl;
    } catch (exception& e) {
        cerr << e.what() << endl;
    }
    return 0;
}

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/193146.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-01 14:59
下一篇 2024-12-01 15:00

相關推薦

發表回復

登錄後才能評論