Reading json files in C++

前端 未结 7 2086
梦如初夏
梦如初夏 2021-02-01 03:17

I\'m trying to read in a JSON file. So far I have focused on using the jsoncpp library. However, the documentation is quite hard to understand for me. Could anyone

7条回答
  •  一生所求
    2021-02-01 04:01

    You can use c++ boost::property_tree::ptree for parsing json data. here is the example for your json data. this would be more easy if you shift name inside each child nodes

    #include                                                              
    #include                                                                
    #include                                                                 
    
    #include                                         
    #include  
     int main () {
    
        namespace pt = boost::property_tree;                                        
        pt::ptree loadPtreeRoot;                                                    
    
        pt::read_json("example.json", loadPtreeRoot);                               
        std::vector> people;      
    
        pt::ptree temp ;                                                            
        pt::ptree tage ;                                                            
        pt::ptree tprofession ;                                                     
    
        std::string age ;                                                           
        std::string profession ;                                                    
        //Get first child                                                           
        temp = loadPtreeRoot.get_child("Anna");                                     
        tage = temp.get_child("age");                                               
        tprofession = temp.get_child("profession");                                 
    
        age =  tage.get_value();                                       
        profession =  tprofession.get_value();                         
        std::cout << "age: " << age << "\n" << "profession :" << profession << "\n" ;
        //push tuple to vector                                                      
        people.push_back(std::make_tuple("Anna", age, profession));                 
    
        //Get Second child                                                          
        temp = loadPtreeRoot.get_child("Ben");                                      
        tage = temp.get_child("age");                                               
        tprofession = temp.get_child("profession");                                 
    
        age =  tage.get_value();                                       
        profession  =  tprofession.get_value();                        
        std::cout << "age: " << age << "\n" << "profession :" << profession << "\n" ;
        //push tuple to vector                                                      
        people.push_back(std::make_tuple("Ben", age, profession));                  
    
        for (const auto& tmppeople: people) {                                       
            std::cout << "Child[" << std::get<0>(tmppeople) << "] = " << "  age : " 
            << std::get<1>(tmppeople) << "\n    profession : " << std::get<2>(tmppeople) << "\n";
        }  
    }
    

提交回复
热议问题