Question

Here is an example feed that I would like to parse: https://gdata.youtube.com/feeds/api/users/aniBOOM/subscriptions?v=2&alt=json

You can check it with http://json.parser.online.fr/ to see what it contains.

I have a small problem while parsing data feed provided by youtube. First issue was the way the youtube provided the data wrapped inside feed field and because of that I couldn't parse the username straight from original json file so I had to parse first entry field and generate new Json data from that.

Anyway the problem is that for some reason that doesn't include more than the first username and I don't know why because if you check that feed on online parser the entry should contain all the usernames.

`

        data = value["feed"]["entry"];
        Json::StyledWriter writer;
        std::string outputConfig = writer.write( data );
//This removes [ at the beginning of entry and also last ] so we can treat it as a Json data
        size_t found;
        found=outputConfig.find_first_of("[");
        int sSize = outputConfig.size();            
        outputConfig.erase(0,1);
        outputConfig.erase((sSize-1),sSize);

        reader.parse(outputConfig, value2, false);

        cout << value2 << endl;

        Json::Value temp;
        temp = value2["yt$username"]["yt$display"];
        cout << temp << endl;

        std::string username = writer.write( temp );
        int sSize2 = username.size();           
        username.erase(0,1);
        username.erase((sSize2-3),sSize2);

` But for some reason [] fix also cuts the data I'm generating, if I print out the data without removing [] I can see all the users but in that case I can't extract temp = value2["yt$username"]["yt$display"];

Was it helpful?

Solution

In JSON, the brackets denote Arrays (nice reference here). You can see this in the online parser, also -- Objects (items with one or more key/value pairs {"key1": "value1", "key2": "value2"}) are denoted with blue +/- signs and Arrays (items inside brackets separated by commas [{arrayItem1}, {arrayItem2}, {arrayItem3}]) are denoted with red +/- signs.

Since entry is an Array, you should be able to iterate through them by doing something like this:

// Assumes value is a Json::Value 
Json::Value entries = value["feed"]["entry"];

size_t size = entries.size();
for (size_t index=0; index<size; ++index) {
    Json::Value entryNode = entries[index];
    cout << entryNode["yt$username"]["yt$display"].asString() << endl;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top