阅读与升压

问题描述:

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读取文件,并将字符串(行)读入向量,然后解析向量的内容。

类粒子是这样的:

class Particle 
{ 

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

为的get/set其它方法在课堂上省略。

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

码主:

int main(){ 

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

你看看升压JSON解析器:http://www.boost.org /doc/libs/1_53_0/doc/html/boost_propertytree/parsers.html#boost_propertytree.parsers.json_parser? – 2013-03-04 16:55:29

+0

是的,但我无法绕过它... – user3728501 2013-03-04 16:56:12

+1

而这个答案http://*.com/a/12735086/667433没有帮助吗? – 2013-03-04 16:57:24

您可以用下面的代码迭代:

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. 
} 

希望它可以帮助

+0

伊特有第一个和第二个......但他们是什么?还有三分之一吗? – user3728501 2013-03-04 17:06:37

+1

@EdwardBird:'iter'是一对,所以没有第三个。 'v.first'是一个持有父节点的'std :: string','v.second'是'boost :: property_tree :: ptree',它可以用来解析对象的字段。您可能需要递归遍历树来获取所有值,具体取决于值的埋藏程度。 – 2013-03-04 17:18:36

+0

啊好吧谢谢,这有帮助。 – user3728501 2013-03-04 18:04:30

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

{ 
    "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; 
} 

修改,你认为合适。

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

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

我没有觉得这有帮助 - '没有这样的节点(particles.electron)'。对于任何人谁不熟悉,'的std :: stringstream的ss'应该被删除,而'提振:: property_tree :: read_json(SS,PT);用''替代的boost :: property_tree :: read_json(,pt);' – user3728501 2013-03-04 21:58:06

+0

@EdwardBird:'read_json'需要一个流,它在boost的文档中。您不能正确填充它。 – 2013-03-04 22:21:26

+0

你有什么建议。 (请参阅我的编辑主)编辑:我没有什么可以添加在主...你有什么建议? – user3728501 2013-03-04 22:22:55

只是纠正问题与上面的答案,但我不能得到正确格式化的评论:

#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; 
} 
+0

还,程序没有运行错误是“‘打印’在此范围内没有宣布”我试图去改变它给printf,但没有奏效。 – 2017-12-05 14:00:28

+0

打印定义在使用ptree类型不打印的主函数下面。也许你忘了复制上面的全部内容。我现在没有电脑可以重新测试。 – Jim 2017-12-05 17:30:30

+0

也许打印需要粘贴在主要上方 – Jim 2017-12-05 17:31:15