简体   繁体   English

C ++:美化boost ptree json_parser

[英]C++: Beautify boost ptree json_parser

In the following code using C++ Boost property tree, I expect a beautiful output such as 在下面的使用C ++ Boost属性树的代码中,我希望得到一个漂亮的输出,例如

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

while in reality I receive: 实际上,我收到:

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

Is there any built-in method to beautify this json result? 是否有任何内置方法可美化此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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM