所有key 获取json_C++那些事之轻松解析json
时间: 2023-08-22 13:07:49 浏览: 46
要轻松解析 JSON,可以使用 C++ 的第三方库来处理 JSON 数据。以下是一些常用的 C++ JSON 解析库:
1. RapidJSON:RapidJSON 是一个快速解析 JSON 的 C++ 库,支持 SAX 和 DOM 解析模式。
2. nlohmann/json:nlohmann/json 是一个现代 C++11 JSON 库,使用简单且易于集成。
3. JsonCpp:JsonCpp 是一个 C++ JSON 库,支持 SAX 和 DOM 解析模式。
使用这些库中的任何一个,您都可以轻松解析 JSON 数据。以下是一个使用 RapidJSON 的示例:
```c++
#include <iostream>
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
using namespace rapidjson;
int main() {
const char* json = "{\"hello\": \"world\"}";
Document document;
document.Parse(json);
Value& s = document["hello"];
std::cout << s.GetString() << std::endl;
return 0;
}
```
在此示例中,我们使用 RapidJSON 解析了一个包含 "hello" 键和 "world" 值的 JSON 对象,并输出了值 "world"。