【问题标题】:Parsing youtube data with C++ and Jsoncpp使用 C++ 和 Jsoncpp 解析 youtube 数据
【发布时间】:2012-03-21 05:37:39
【问题描述】:

这是我要解析的示例提要: 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"];

【问题讨论】:

    标签: c++ json youtube youtube-api jsoncpp


    【解决方案1】:

    在 JSON 中,方括号表示数组(很好的参考 here)。您可以在在线解析器中看到这一点,也 - 对象(具有一个或多个键/值对 {"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;
    }
    

    【讨论】:

    • 是的,我不知道可以将其视为数组...您的示例完全解决了我的问题。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-17
    • 2018-04-27
    • 1970-01-01
    • 2014-09-13
    • 1970-01-01
    • 2012-11-27
    • 2013-02-12
    相关资源
    最近更新 更多