zoukankan      html  css  js  c++  java
  • boost::ptree;boost::json_parser

    {
        "Version": 1,
        "Metrics": [{
            "wingarea": 1341.01,
            "unit": "FT2"
        }, {
            "wingspan": 1350.81,
            "unit": "Inch"
        }],
        "Propulsion": {
            "Location": {
                "XEDIC": 22.618,
                "unit": "FT"
            }
        }
    }
    #include <cstdio>
    #include <iostream>
    #include <sstream>
    #include <vector>
    #include <string>
    #include <set>
    #include <exception>
    #include <iostream>
    #include <boost/property_tree/ptree.hpp>
    #include <boost/property_tree/xml_parser.hpp>
    #include <boost/property_tree/json_parser.hpp>
    #include <boost/foreach.hpp>
    #include <boost/typeof/typeof.hpp>
    using namespace std;
    using namespace boost;
    namespace pt = boost::property_tree;
    
    int main()
    {
        pt::ptree tree;
    
        string m_file = "./ss.json";
    
        try
        {
            // 创建ptree对象
            boost::property_tree::ptree json_root;
            // 读取file文件,并将根节点存储赋值给json_root
            boost::property_tree::read_json<boost::property_tree::ptree>(m_file, json_root);
    
            //1解析键值对
            cout << json_root.get<int>("Version") <<endl;
    
            //2获取子节点的方法
            boost::property_tree::ptree Metrics = json_root.get_child("Metrics");
            boost::property_tree::ptree::iterator json_iterator = Metrics.begin();
            cout << json_iterator->second.get<double>("wingarea") << endl;
            cout << json_iterator->second.get<string>("unit") << endl;
            json_iterator++;
            cout << json_iterator->second.get<double>("wingspan") << endl;
            cout << json_iterator->second.get<string>("unit") << endl;
    
            //3逐级解析
            boost::property_tree::ptree Propulsion = json_root.get_child("Propulsion");
            boost::property_tree::ptree Location = Propulsion.get_child("Location");
            cout << Location.get<double>("XEDIC") << endl;
            cout << Location.get<string>("unit") << endl;
        }
        catch (std::exception &e)  
        {
            cout << "Error: " << e.what() << endl;
        }
    }
  • 相关阅读:
    WeakReference(弱引用)
    男人怎么挑选女人
    同步方法和异步方法
    常指针与指针常量的区别(转帖)
    关于WebService的一些注意事项
    ASP.Net缓存技术
    关于GridView手动绑定的一段代码,一切尽在不言中
    基本三层架构的一些代码
    写给自己看的关于DataList的和RePeater
    ASP.Net绑定数据源
  • 原文地址:https://www.cnblogs.com/osbreak/p/14497189.html
Copyright © 2011-2022 走看看