C++:美化boost ptree json_parser
C++: Beautify boost ptree json_parser
在以下使用 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 属性 树的代码中,我希望得到一个漂亮的输出,例如
{
"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);