gpt4 book ai didi

C++:美化boost ptree json_parser

转载 作者:行者123 更新时间:2023-11-28 05:03:03 26 4
gpt4 key购买 nike

在以下使用 C++ Boost 属性树的代码中,我希望得到一个漂亮的输出,例如

{
"fruit": {
"apple": "true",
"orange": "true"
},
"animal": {
"cat": "true",
"dog": "true",
"bird": {
"duck": "true"
}
}
}

而实际上我收到:

{"fruit":{"apple":"true","orange":"true"},"animal":
{"cat":"true","dog":"true","bird":{"duck":"true"}}}

有没有内置的方法来美化这个json结果?

#include <iostream>
#include <string>
#include <sstream>

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>

using boost::property_tree::ptree;
using boost::property_tree::basic_ptree;


int main()
{
ptree root;

root.put("fruit.apple", "true");
root.put("fruit.orange", "true");
root.put("animal.cat", "true");
root.put("animal.dog", "true");
root.put("animal.bird.duck", "true");

std::ostringstream buf;
write_json(buf, root, false);
buf << std::endl;

std::string json = buf.str();
std::cout<<json<<std::endl;

return 0;
}

最佳答案

您是否尝试过为 pretty 参数传递 true

write_json(buf, root, true);

关于C++:美化boost ptree json_parser,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45487818/

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