3

这是我想解析的示例提要: https ://gdata.youtube.com/feeds/api/users/aniBOOM/subscriptions?v=2&alt=json

您可以通过http://json.parser.online.fr/查看它包含的内容。

我在解析 youtube 提供的数据提要时遇到了一个小问题。第一个问题是 youtube 提供包含在 feed 字段中的数据的方式,因此我无法直接从原始 json 文件解析用户名,所以我必须解析第一个输入字段并从中生成新的 Json 数据。

无论如何,问题是由于某种原因不包括第一个用户名,我不知道为什么,因为如果您在在线解析器上检查该提要,则该条目应包含所有用户名。

`

        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);

` 但由于某种原因,[] 修复也削减了我正在生成的数据,如果我在不删除 [] 的情况下打印出数据,我可以看到所有用户,但在这种情况下,我无法提取 temp = value2["yt$username "]["yt$display"];

4

1 回答 1

3

在 JSON 中,方括号表示数组(这里很好的参考)。您还可以在在线解析器中看到这一点——对象(具有一个或多个键/值对的项目{"key1": "value1", "key2": "value2"})用蓝色 +/- 符号表示,数组(括号内的项目用逗号分隔[{arrayItem1}, {arrayItem2}, {arrayItem3}])用红色 +/- 符号表示.

由于 entry 是一个数组,因此您应该能够通过执行以下操作来遍历它们:

// 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;
}
于 2012-03-21T21:02:39.013 回答