How to iterate a boost property tree?

前端 未结 5 707
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-24 03:27

I am know approaching to boost property tree and saw that it is a good feature of boost libs for c++ programming.

Well, I have one doubt? how to iterate a property t

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-24 04:11

    Here is what I came up with after much experimentation. I wanted to share it in the community because I couldn't find what I wanted. Everybody seemed to just post the answer from the boost docs, which I found to be insufficient. Anyhow:

    #include 
    #include 
    #include 
    #include 
    
    using namespace std; 
    using boost::property_tree::ptree; 
    
    string indent(int level) {
      string s; 
      for (int i=0; ifirst << "\": "; 
    
          printTree(pos->second, level + 1); 
          ++pos; 
          if (pos != pt.end()) {
            cerr << ","; 
          }
          cerr << endl;
        } 
    
       cerr << indent(level) << " }";     
      }
    
      return; 
    }
    
    int main(int, char*[]) {
    
      // first, make a json file:
      string tagfile = "testing2.pt"; 
      ptree pt1;
      pt1.put("object1.type","ASCII");  
      pt1.put("object2.type","INT64");  
      pt1.put("object3.type","DOUBLE");  
      pt1.put("object1.value","one");  
      pt1.put("object2.value","2");  
      pt1.put("object3.value","3.0");  
      write_json(tagfile, pt1); 
    
      ptree pt;
      bool success = true; 
    
      try {
          read_json(tagfile, pt); 
          printTree(pt, 0); 
          cerr << endl; 
      }catch(const json_parser_error &jpe){
          //do error handling
          success = false
      }
    
      return success; 
    }
    

    Here is the output:

    rcook@rzbeast (blockbuster): a.out
    {
      "object1": 
      {
        "type": "ASCII",
        "value": "one"
       },
      "object2": 
      {
        "type": "INT64",
        "value": "2"
       },
      "object3": 
      {
        "type": "DOUBLE",
        "value": "3.0"
       }
     }
    rcook@rzbeast (blockbuster): cat testing2.pt 
    {
        "object1":
        {
            "type": "ASCII",
            "value": "one"
        },
        "object2":
        {
            "type": "INT64",
            "value": "2"
        },
        "object3":
        {
            "type": "DOUBLE",
            "value": "3.0"
        }
    }
    

提交回复
热议问题