gpt4 book ai didi

json - 如何将 boost 属性树输出为 JSON 编码字符串?

转载 作者:行者123 更新时间:2023-12-01 21:59:10 24 4
gpt4 key购买 nike

有时,创建 JSON 编码字符串来表示和交换数据非常有用。将 Boost 属性树编码为 JSON 字符串的最佳方式是什么?

最佳答案

以下是执行该任务的示例代码:

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/optional.hpp>
#include <iostream>
#include <sstream>
#include <cstdlib>

int main()
{
boost::property_tree::ptree pt;
pt.put("Test", "string");
pt.put("Test2.inner0", "string2");
pt.put("Test2.inner1", "string3");
pt.put("Test2.inner2", 1234);

std::stringstream ss;
boost::property_tree::json_parser::write_json(ss, pt);

std::cout << ss.str() << std::endl;

return 0;
}

使用 GCC 编译此代码:

g++ main.cpp -lboost_system -o SamplePT_JSON

这是预期的输出:

{
"Test": "string",
"Test2":
{
"inner0": "string2",
"inner1": "string3",
"inner2": "1234"
}
}

关于json - 如何将 boost 属性树输出为 JSON 编码字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36868297/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com