如何在 C++ 中使用 Boost.JSON 从 std::string 解析 JSON
parse_snippet.cpp
#include <boost/json.hpp>
boost::json::value j = boost::json::parse(json_str);完整示例:
json_example.cpp
#include <boost/json.hpp>
#include <iostream>
int main() {
// 硬编码的 JSON 字符串
const std::string json_str = R"(
{
"key": "1234",
"value": "Hello, world!"
}
)";
// 解析 JSON 字符串
boost::json::value j = boost::json::parse(json_str);
// 访问 "value" 属性并打印到 stdout
std::string value = j.at("value").as_string().c_str();
std::cout << value << std::endl; // 打印 "Hello, world!"
return 0;
}使用以下命令编译
build_json.sh
g++ -o json json.cpp -lboost_jsonIf this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow