使用boost读取json文件 [英] Reading json file with boost

查看:823
本文介绍了使用boost读取json文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的档案:

[data.json]

[data.json]

{
    "electron": {
        "pos": [0,0,0],
        "vel": [0,0,0]
    },

    "proton": {
        "pos": [1,0,0],
        "vel": [0,0.1,0]
    },

     "proton": {
        "pos": [-1,0,0],
        "vel": [0,-0.1,-0.1]
    }
}

如何通过解析此文件创建一个粒子向量。根据我的理解,我需要使用boost读取文件,读取字符串(行)到一个向量,然后解析向量的内容。

How do I create a vector of particles from parsing this file. As I understand it I need to read the file using boost and read the strings (lines) into a vector, and then parse the contents of the vector.

类粒子是这样的:

class Particle
{

    private:
    particle_type mtype; // particle_type is an enum
    vector<double> mPos;
    vector<double> mVel;
};

类中省略了get / set的其他方法。

Other methods for get/set have been omitted in the class.

基本上,我想帮助创建一个向量< Particle> ,其中正确的位置和速度数据和particle_type数据被解析。提前感谢。

Basically I would like help creating a vector<Particle> with the correct position and velocity data and particle_type data parsed into it. Thanks in advance.

主程式码:

int main(){

    boost::property_tree::ptree pt;
    boost::property_tree::read_json("data.json", pt);
}


推荐答案

。稍微未经测试的代码。

I modified your JSON a bit. Slightly untested code.

{
    "particles": [
        {
            "electron": {
                "pos": [
                    0,
                    0,
                    0
                ],
                "vel": [
                    0,
                    0,
                    0
                ]
            },
            "proton": {
                "pos": [
                    -1,
                    0,
                    0
                ],
                "vel": [
                    0,
                    -0.1,
                    -0.1
                ]
            }
        }
    ]
}

...



...

#ifdef _MSC_VER
#include <boost/config/compiler/visualc.hpp>
#endif
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/foreach.hpp>
#include <cassert>
#include <exception>
#include <iostream>
#include <sstream>
#include <string>

int main()
{
    try
    {
        std::stringstream ss;
        // send your JSON above to the parser below, but populate ss first


        boost::property_tree::ptree pt;
        boost::property_tree::read_json(ss, pt);

        BOOST_FOREACH(boost::property_tree::ptree::value_type &v, pt.get_child("particles.electron"))
        {
            assert(v.first.empty()); // array elements have no names
            std::cout << v.second.data() << std::endl;
            // etc
        }
        return EXIT_SUCCESS;
    }
    catch (std::exception const& e)
    {
        std::cerr << e.what() << std::endl;
    }
    return EXIT_FAILURE;
}

按照您的需要修改。

打印整个树以查看正在读取的内容。这有助于调试。

Print the entire tree to see what is being read. This helps in debugging.

void print(boost::property_tree::ptree const& pt)
{
    using boost::property_tree::ptree;
    ptree::const_iterator end = pt.end();
    for (ptree::const_iterator it = pt.begin(); it != end; ++it) {
        std::cout << it->first << ": " << it->second.get_value<std::string>() << std::endl;
        print(it->second);
    }
}

这篇关于使用boost读取json文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆