首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用boost读取json文件

使用boost读取json文件
EN

Stack Overflow用户
提问于 2013-03-05 00:53:03
回答 3查看 91.2K关注 0票数 27

我有一个这样的文件:

data.json

代码语言:javascript
复制
{
    "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读取文件,并将字符串(行)读入向量,然后解析向量的内容。

类粒子是这样的:

代码语言:javascript
复制
class Particle
{

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

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

基本上,我希望帮助创建一个正确的位置和速度数据和particle_type数据解析到它的vector<Particle>。提前谢谢。

main中的代码:

代码语言:javascript
复制
int main(){

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

回答 3

Stack Overflow用户

发布于 2013-03-05 01:09:54

我稍微修改了一下你的JSON。稍微未经测试的代码。

代码语言:javascript
复制
{
    "particles": [
        {
            "electron": {
                "pos": [
                    0,
                    0,
                    0
                ],
                "vel": [
                    0,
                    0,
                    0
                ]
            },
            "proton": {
                "pos": [
                    -1,
                    0,
                    0
                ],
                "vel": [
                    0,
                    -0.1,
                    -0.1
                ]
            }
        }
    ]
}

..。

代码语言:javascript
复制
#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;
}

按您认为合适的方式修改。

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

代码语言:javascript
复制
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);
    }
}
票数 27
EN

Stack Overflow用户

发布于 2013-03-05 01:04:15

您可以使用以下代码进行迭代:

代码语言:javascript
复制
boost::property_tree::basic_ptree<std::string,std::string>::const_iterator iter = pt.begin(),iterEnd = pt.end();
for(;iter != iterEnd;++iter)
{
     iter->first; // Your key, at this level it will be "electron", "proton", "proton"
     iter->second; // The object at each step {"pos": [0,0,0], "vel": [0,0,0]}, etc.
}

希望能有所帮助

票数 7
EN

Stack Overflow用户

发布于 2017-03-22 05:38:43

我只是在更正上面答案的问题,但我无法在评论中获得正确的格式:

代码语言:javascript
复制
#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>

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);
    }
}

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

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


        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"))
        {
            assert(v.first.empty()); // array elements have no names
            print(v.second);
        }
        return EXIT_SUCCESS;
    }
    catch (std::exception const& e)
    {
        std::cerr << e.what() << std::endl;
    }
    return EXIT_FAILURE;
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15206705

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档