Search code examples
c++jsonparsingboostboost-propertytree

Boost::ptree - Accessing a property tree node contained in a list


I'm trying go get data from some weather API which returns a JSON. I read that every item in a list is considered as a node without "label", but, here, inside the list are contained two nodes. How do I access to the description label, since root.get<string>("weather.description") throws a Node does not exist error ?

What I tried (Which returned nothing):

for (auto it: root.get_child("weather")) {
    cout << it.first.data() << "+";
    cout << it.second.data() << endl;
}

weather.json:

{
    "weather": [
        {
            "id": "701",
            "main": "Mist",
            "description": "brume",
            "icon": "50n"
        },
        {
            "id": "502",
            "main": "Sun",
            "description": "soleil",
            "icon": "50b"
        }
    ]
}

Solution

  • Found a workaround for this! I thought weather was a list of 8 separate nodes, but it's actually two children of weather. This way, I can access to their individual data with the following:

    for (auto it: root.get_child("weather")) {
        cout << it.second.get_child("description").data() << endl;
    }
    

    Which returns:

    brume
    soleil