2017-08-03 3 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"}}} 

が任意の構築されています - これを美化する方法で 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

あなたはprettyパラメータにtrueを渡してみましたがありますか?

write_json(buf, root, true); 
関連する問題