• 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;
        }
    }
  • 相关阅读:
    JavaScript自定义事件
    用Java构建一个简单的WebSocket聊天室
    PHP实现支付宝小程序用户授权的工具类
    jq ajax超时设置
    gulp使用笔记
    vue学习—组件的定义注册
    echarts设置线条粗细
    求js数组的最大值和最小值
    js删除数组中的 "NaN"
    jq方法(end)
  • 原文地址:https://www.cnblogs.com/osbreak/p/14497189.html
Copyright © 2020-2023  润新知