On 10.08.2011, at 03:34, Scott Smith wrote:

I’m trying to use the boost property tree to read a json file.  I’m using 1.46 on gcc 4.6 (Fedora 15).  I’m also running on Eclipse (Indigo) which is relevant for something I mention below.
 
Here’s the issue .  I have a JSON file which has an array of parameters which looks something like:
{
“config”:
{
                “modules”:
                [
                                {
                                                “name”: “somename0”,
                                                “param1”: “someparam”,
                                                “param2”:”someotherparm”
                                },
                                {
                                                “name”: “somename1”,
                                                “param1”: “someparam”,
                                                “param2”:”someotherparm”
                                },
                                {
                                                “name”: “somename2”,
                                                “param1”: “someparam”,
                                                “param2”:”someotherparm”
                                }
                ]
}
}
 
So, I wrote the following code (Note that I can’t use boost:FOR_EACH because of some bug in eclipse (#332278) which causes an error—or so I believe):
 
                ptree v = pt.get<ptree>(“config.modules”);

This is wrong. You don't use get() for getting a subtree, you use get_child(). This call is guaranteed to fail at runtime, if not at compile time.

                for (ptree::iterator it = v.begin() ; it != v.end ; ++it)
                {
                                ptree module = ???;
                }
 
 
The first two lines seem to compile fine.  I’m assuming that what I get from v is the “root” ptree (the parent) for the various “modules” objects and that the iterator will walk through them.  However, I’ve tried all sorts of things trying to get at the module data (e.g., it->second

This one ought to work. If it doesn't, please post the error message. However, you should capture it in a reference, not make a copy - deep-copying ptrees is pretty expensive.

Sebastian