2017-08-03 100 views
0

在下面的代码用C++升压属性树,我想到一个美丽的输出如C++:美化提升ptree中json_parser

{ 
    "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"}}} 

是否有任何内置-in方法来美化这个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; 
} 

回答

1

您是否尝试过通过truepretty参数?

write_json(buf, root, true);