#include <iostream>#include<boost/property_tree/ptree.hpp>#include<boost/property_tree/json_parser.hpp>using namespacestd;using namespaceboost::p roperty_tree;//It's a bit like std::list<string,ptree> you can construct a node of any type, special array type, too flexible to use.intMain () {std::stringJSON ="{\ "a\": 1,\ "b\": {\ "c\": 2,\ "d\": 3},\ "e\": [3,4]}"; Boost::p roperty_tree::p tree pt,child1,child2; Std::stringstream SS (JSON); Boost::p Roperty_tree::read_json (SS, PT); Child1= Pt.get_child ("B"); //for Tree Traversal for(Auto c:child1) {cout<< C.first<<c.second.data () <<endl;//This will print out the first} child2= Pt.get_child ("E"); for(Ptree::iterator it = Child2.begin (); It! = Child2.end (); + +it) {Auto Pt1= it->second;//First is empty for(Auto c:pt1)//It seems that there are brackets, whether [] or {} is Ptree, traversing the tree, feeling this is not used{cout<<c.first<<C.second.data (); } cout<< pt1.Get<int> ("F");//based on node F, this second is still a tree {"F": 1}cout<< pt1.get_value<int> ();//This is not the way to take the value, but this can, is also the array [1,2,3,4,5], it should be second directly is his value//iterate through the array F-value, get directly the node value inside the tree} system ("Pause"); return 0;}//Jck Self-Solution: There is a parenthesis, need a ptree. #include <iostream>#include<string>#include<sstream>#include<boost/property_tree/ptree.hpp>#include<boost/property_tree/json_parser.hpp>intMain () {boost::p roperty_tree::p tree ptree_root; Ptree_root.put ("Root","Me_root");//put Key,value, is ordinary to a ptree inside a key value pairboost::p roperty_tree::p tree ptree_items; {boost::p roperty_tree::p Tree pt; //a tree /*pt.put<int> ("item_1");*/Pt.put<int> ("item_2", +); Ptree_items.push_back (Make_pair ("", PT));//the equivalent of a vector, as a subtrees tree} {boost::p roperty_tree::p tree pt;//another tree .Pt.put ("item_1"," -"); Pt.put ("item_2"," -"); Ptree_items.push_back (Make_pair ("", PT)); } ptree_root.put_child ("Items", Ptree_items);//as a child nodeboost::p Roperty_tree::write_json ("C:\\file_path.json", Ptree_root); GetChar ();}
It's a good look today, I've written before, and I'm dizzy.