加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 19:24:27 所属栏目:百科 来源:网络整理
导读:CJSON 下载地址以及介绍:http://sourceforge.net/projects/cjson/ bool ParseResourceString(CString StrRes,std::vectorstd::string VecImgUrl, std::string VideoUrl){ cJSON* JsonObj=cJSON_Parse(StrRes); if (!JsonObj) { CString info; info.Format(

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"}]";

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读