简体   繁体   English

使用boost属性树解析JSON

[英]Parsing JSON with boost property tree

I'm building an application that gets movie information from themoviedb.com. 我正在构建一个从themoviedb.com获取电影信息的应用程序。 The information is provided in a JSON file. 该信息在JSON文件中提供。 I'm trying to store the information using boost property tree. 我正在尝试使用boost属性树存储信息。 But There is a little problem. 但有一点问题。

I illustrate the problem by the following code: 我通过以下代码说明了这个问题:

#include <vector>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/foreach.hpp>

using namespace std;
using boost::property_tree::ptree;

class single_t{
    int             sID;
    string          sName;
public:
    void            setID(int ID){sID=ID;}
    int             getID(){return sID;}
    void            setName(string Name){sName=Name;}
    string          getName(){return sName;}
};

typedef vector<single_t*> multiple_t;

class foo{
    string          fTitle;
    multiple_t      fItems;
public:
    string          getTitle(){return fTitle;}
    void            setTitle(string Title){fTitle=Title;}
    multiple_t      getItems(){return fItems;}
    void            setItems(multiple_t Items){fItems = Items;}
    void            setItems(single_t Item){fItems.push_back(&Item);}
};

int main () {
    try{
        string response = "{\"title\":\"Foo\",\"items\":[{\"id\":123,\"name\":\"test1\"},{\"id\":456,\"name\":\"test2\"}]}";

        ptree pt;
        stringstream ss; ss << response;
        read_json(ss, pt);
        foo results;
        results.setTitle(pt.get<string>("title"));
        BOOST_FOREACH(ptree::value_type &v,pt.get_child("items")){
            single_t result;
            result.setID(v.second.get<int>("id"));
            result.setName(v.second.get<string>("name"));
            results.setItems(result);
        }
        cout << "Tilte: " << results.getTitle() << endl;
        cout << "Items:" << endl;
        for (int i=0; i!=results.getItems().size(); i++) {
            cout << "\tID: " << results.getItems()[i]->getID()<< endl;
            cout << "\tName: " << results.getItems()[i]->getName()<< endl;
        }
    }
    catch (exception& e)
    {
        cout << "Exception: " << e.what();
    }

}

But when I run this I get the following output: 但是当我运行它时,我得到以下输出:

Tilte: Foo
Items:
  ID: 456
  Name: test2
  ID: 456
  Name: test2

Does anyone know what I'm doing wrong? 有谁知道我做错了什么? I guess it is in the BOOST_FOREACH code. 我想这是在BOOST_FOREACH代码中。

PS: Using Xcode 4.5.2 with LLVM GCC 4.2 Compiler. PS:使用Xcode 4.5.2和LLVM GCC 4.2编译器。

Problem is not with property_tree , problem is, that you try store pointer to local variable in vector. 问题不在于property_tree ,问题是,你尝试在向量中存储指向局部变量的指针。 You can save by value, or use some smart pointer (for example boost::shared_ptr ). 您可以按值保存,或使用一些智能指针(例如boost::shared_ptr )。

Problem: 问题:

void            setItems(single_t Item){fItems.push_back(&Item);}

After exit from this function, local variable Item will be destroyed, so you have dangling pointer. 退出此函数后,将破坏局部变量Item ,因此您有悬空指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM