美文网首页工作生活
rapidjson嵌套数组遍历

rapidjson嵌套数组遍历

作者: 小怪兽狂殴奥特曼 | 来源:发表于2019-07-03 15:45 被阅读0次
#include "rapidjson/document.h"        // rapidjson's DOM-style API  
#include "rapidjson/prettywriter.h"    // for stringify JSON  
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"

#define GET_JSON_ARRAY_NODE(father, name, node) {\
    if((father).HasMember(name.c_str())) { \
        node = (father)[name.c_str()]; \
        if(!node.IsArray()) { \
            return -1; \
        } \
    } \
}


int main()
{
    std::string req = "{\"errorcode\":\"0\",\"errormsg\":\"success\",\"data\":[{\"channelId\":\"001\",\"channel\":\"电影\",\"list\":[{\"title\":\"过亿票房专区\",\"sTitle\":\"恶补票房过亿好片\",\"channelId\":\"001\",\"pictureUrl\":\"http://putv.qpic.cn/3337/20185/648364\",\"postUrl\":\"http://putv.qpic.cn/1973/20181/496722\",\"sourceId\":\"9\",\"vid\":\"p72z62n28wlcamx\"},{\"title\":\"黄河绝恋\",\"sTitle\":\"宁静演绎战火中的爱情故事\",\"channelId\":\"001\",\"pictureUrl\":\"http://putv.qpic.cn/2456/19045/648364\",\"postUrl\":\"http://putv.qpic.cn/8211/19043/496722\",\"sourceId\":\"9\",\"vid\":\"lqzx2f9o5egks3t\"},{\"title\":\"厉害了,我的国\",\"sTitle\":\"光影记录彰显大国实力\",\"channelId\":\"001\",\"pictureUrl\":\"http://putv.qpic.cn/1939/29857/648364\",\"postUrl\":\"http://putv.qpic.cn/1555/29856/496722\",\"sourceId\":\"9\",\"vid\":\"1jj78hgru2so83n\"}]},{\"channelId\":\"002\",\"channel\":\"电视剧\",\"list\":[{\"title\":\"共和国血脉\",\"sTitle\":\"热血励志战地爱情史诗\",\"channelId\":\"002\",\"pictureUrl\":\"http://putv.qpic.cn/2788/8364/648364\",\"postUrl\":\"http://putv.qpic.cn/1876/18294/496722\",\"sourceId\":\"9\",\"vid\":\"3117dc0ns535f4g\"},{\"title\":\"哥哥姐姐的花样年华\",\"sTitle\":\"王雅捷王挺演绎小人物的励志人生\",\"channelId\":\"002\",\"pictureUrl\":\"http://putv.qpic.cn/5582/22499/648364\",\"postUrl\":\"http://putv.qpic.cn/7492/22496/496722\",\"sourceId\":\"9\",\"vid\":\"47t5p91a8ddeg66\"},{\"title\":\"那片花那片海\",\"sTitle\":\"下海创业成功\",\"channelId\":\"002\",\"pictureUrl\":\"http://putv.qpic.cn/8798/31073/648364\",\"postUrl\":\"http://putv.qpic.cn/4712/31072/496722\",\"sourceId\":\"9\",\"vid\":\"7m02tg6cbpafqkl\"}]}]}";

    rapidjson::Document root;
    root.Parse(req.c_str());
    if (root.HasParseError() || !root.IsObject())  
    {  
        return -1;
    } 

    std::string nodename = "data";
    rapidjson::Value list;
    GET_JSON_ARRAY_NODE(root, nodename, list);
    //外层数组遍历
    for (Value::ConstValueIterator itr = list.Begin(); itr != list.End(); ++itr)
    {
        if(!itr->IsObject())
        {
            return -1;
        }

        nodeName = "list";
        if((*itr).HasMember(nodeName.c_str())) 
        {
            //只能获得引用
            const rapidjson::Value &lst = (*itr)[nodeName.c_str()];
            if(lst.IsArray()==false)
            {
                return -1;
            }
            //内层数组遍历
            for (Value::ConstValueIterator it = lst.Begin(); it != lst.End(); ++it)
            {
                //do something
            }
        }        
    }
}

相关文章

  • rapidjson嵌套数组遍历

  • angular2foreach遍历的几种用法

    遍历简单的数组 遍历数组对象 遍历嵌套数组

  • js 数组去重

    方法一 For嵌套for 使用splice去重更改原数组 正向遍历循环 遇到删掉 原数组递减1 优点:该方法可以顾...

  • Easy_01

    两数之和 想法一:最直接的暴力解法。通过两层嵌套循环对数组进行遍历。 vector twoSum(vector &...

  • 过滤存在嵌套的数组,并将符合的返回,原格式不变

    利用JSON将原数组拷贝一份,这样可以避免原数组被改变,再每个元素中遍历元素中嵌套的数组,将符合条件的返回给当前的...

  • VS常用四种遍历数组的方法

    目录 for 遍历数组 for in 遍历数组 for of 遍历数组 forEach遍历数组 优缺点总结原文:h...

  • foreach/forin

    1.for/in遍历属性,数组是遍历下标 for/each遍历属性值,数组遍历数组的值

  • JS数组遍历的三种常用方法

    1.数组下标遍历 数组下标遍历是最常用也最普通的数组遍历方式 例如: 2.for in遍历 for in是根据数组...

  • for_of循环

    for(let value of target){}循环遍历 遍历数组 遍历Set 遍历Map 遍历字符串 遍历伪数组

  • PHP中的数组

    数组分类 索引数组 关联数组 数组遍历 传值遍历 传址遍历 数组函数 指针操作函数 current($array)...

网友评论

    本文标题:rapidjson嵌套数组遍历

    本文链接:https://www.haomeiwen.com/subject/pwxchctx.html