boost:property_tree 学习笔记

先放资料:

How to use boost::property_tree to load and write JSON

How to iterate a boost property tree?

不出现key的方法遍历一个json文件:

 1   /* ***********************************************
 2   Author :111qqz
 3   mail: renkuanze@sensetime.com
 4   Created Time :2018年08月17日 星期五 15时29分23秒
 5   File Name :ptree.cpp
 6   ************************************************ */
 7   #include <boost/property_tree/ptree.hpp>
 8   #include <boost/property_tree/json_parser.hpp>
 9   #include <bits/stdc++.h>
10   #include <vector>
11   using namespace std;
12   using boost::property_tree::ptree;
13   
14   string indent(int level) {
15     string s; 
16     for (int i=0; i<level; i++) s += "  ";
17     return s; 
18   } 
19   
20   void printTree (ptree &pt, int level) {
21     if (pt.empty()) {
22       cerr << "\""<< pt.data()<< "\"";
23     }
24   
25     else {
26       if (level) cerr << endl; 
27   
28       cerr << indent(level) << "{" << endl;     
29   
30       for (ptree::iterator pos = pt.begin(); pos != pt.end();) {
31         cerr << indent(level+1) << "\"" << pos->first << "\": "; 
32   
33         printTree(pos->second, level + 1); 
34         ++pos; 
35         if (pos != pt.end()) {
36           cerr << ","; 
37         }
38         cerr << endl;
39       } 
40   
41      cerr << indent(level) << " }";     
42     }
43   
44     return; 
45   }
46   int main()
47   {
48   	ptree root;
49   	read_json("terr.json",root);
50   	printTree(root,0);
51   
52   
53   	return 0;
54   
55   }
56   
57