#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
}
}
}
}
网友评论