微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

【拿走主义】CJSON 解析数组详解

CJSON 下载地址以及介绍:http://sourceforge.net/projects/cjson/

bool ParseResourceString(CString StrRes,std::vector<std::string>& Vecimgurl,\
                                          std::string& VideoUrl)
{
    cJSON* JsonObj=cJSON_Parse(StrRes);

    if (!JsonObj)
    {
        CString info;
        info.Format("Json Error before: [%s]",cJSON_GetErrorPtr());
        HT_DebugOutCString(info);
        return false;
    }

    if (cJSON_Array != JsonObj->type)
    {
        HT_DebugOutCString(CString("CJSON NOT AN Array"));
        return false;
    }

    int size = cJSON_GetArraySize(JsonObj);

    for (int index = 0; index < size; index++)
    {
        cJSON* item = cJSON_GetArrayItem(JsonObj,index);

        if (NULL == item)
        {
            HT_DebugOutCString(CString("CJson item null"));
            return false;
        }

        if (cJSON_Object != item->type )
        {
            HT_DebugOutCString(CString("CJson SubArray NOT AN Object"));
            return false;
        }

        cJSON* JsonSubArrayContent = item->child;

        // GetVideoUrl
        if (std::string(JsonSubArrayContent->valuestring) == "video" \
            && VideoUrl.empty() && JsonSubArrayContent->next && \
            std::string(JsonSubArrayContent->next->string) == "url")
        {
            VideoUrl = JsonSubArrayContent->next->valuestring;
        }

        // GetPhotoUrl
        if (std::string(JsonSubArrayContent->valuestring) == "photo" \
            && JsonSubArrayContent->next && Vecimgurl.size() < MAX_PHOTO_NUM && \
            std::string(JsonSubArrayContent->next->string) == "url")
        {
            Vecimgurl.push_back(std::string(JsonSubArrayContent->next->valuestring));
        }
    }

    return true;
}


测试:

    CString test = "[{\"type\":\"video\",\"url\":\"/image/vrb2/i2/bad758759ddd47f89841366fcd02ee82/00028?key=7155&offset=2721385644&high=9744\"},\
                   {\"type\":\"photo\",\"url\":\"/image/vrb2/i2/bad758759ddd47f89841366fcd02ee82/00028?key=7152&offset=2721368861&high=8823\"},\"url\":\"/image/vrb2/i2/bad758759ddd47f89841366fcd02ee82/00028?key=714f&offset=2721135445&high=8696\"},\"url\":\"/image/vrb2/i2/bad758759ddd47f89841366fcd02ee82/00028?key=714e&offset=2721125841&high=9524\"}]";

原文地址:https://www.jb51.cc/json/290540.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐