当前位置:首页 » 《随便一记》 » 正文

RapidJSON解析数组_GreedySnaker的博客

7 人参与  2022年05月12日 08:36  分类 : 《随便一记》  评论

点击全文阅读


一 简介

RapidJSON是腾讯开源的一个高效的C++ JSON解析器及生成器,它是只有头文件的C++库,所以使用cmake非常好用。RapidJSON是跨平台的,支持Windows, Linux, Mac OS X及iOS, Android。
GitHub地址

二 经验

json串

{"info": 
	{"description": "This is v1.0 of the VQA dataset.", 
	"license": 
			{"questions":[
						{"question": "What is the table made of?", 
						"image_id": 350623, 
						"question_id": 3506232}, 
			{"question": "Is the food napping on the table?",
			"image_id": 350623, 
			"question_id": 3506230}]} 
	}
} 

解析获得questions中的question


#include "rapidjson/document.h"
#include "rapidjson/prettywriter.h"
#include "rapidjson/stringbuffer.h"
#include <iostream>
#include <fstream>
#include <string>
int main()
{
	rapidjson::Document document; // 定义一个Document对象
    std::string str = "";
    document.Parse(str.c_str()); // 解析,Parse()无返回值,也不会抛异常
    if (document.HasParseError()) // 通过HasParseError()来判断解析是否成功
    {
        // 可通过GetParseError()取得出错代码,
        // 注意GetParseError()返回的是一个rapidjson::ParseErrorCode类型的枚举值
        // 使用函数rapidjson::GetParseError_En()得到错误码的字符串说明,这里的En为English简写
        // 函数GetErrorOffset()返回出错发生的位置
        printf("parse error: (%d:%d)%s\n", document.GetParseError(), document.GetErrorOffset(), rapidjson::GetParseError_En(document.GetParseError()));
        return -1;
    }
    else
    {
    	if(document.HasMember("info"))
    	{
    		const rapidjson::Value& obj = document["info"];
    		if(obj.HasMember("license"))
   			{
   				const rapidjson::Value& license= obj["license"];
   				if(license.HasMember("questions")&&
   					license["questions"].IsArray())
 				{
	 				auto questions = license["questions"].GetArray();
	   				for(int i=0; i <questions.Size();i++)
	   				{
	   					if(questions[i].HasMember("question")&& 
	   						questions[i]["question"].IsString())
   						{
   							std::string strQuestion = questions[i]["question"].GetString();
   						}
	   				}
 				}				
   			}
    	}
    }
    return 0;
}

三 总结

不清楚的类型全是auto `····


点击全文阅读


本文链接:http://m.zhangshiyu.com/post/39952.html

解析  出错  函数  
<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

最新文章

  • 相国独子的丫鬟砸坏我的玉佩后,我当场拒婚阅读_玉佩陈郡谢氏全新_小说后续在线阅读_无删减免费完结_
  • 手术时,我看着病人惨死最新试读_淼淼陆知衍姜颜全本完结_小说后续在线阅读_无删减免费完结_
  • 男友霸道给我开黑卡,转头却骂我是捞女最新章节_肖年顾客黑卡热文_小说后续在线阅读_无删减免费完结_
  • 他在回忆尽头全集_贺南舟许清梨叶絮完结txt_小说后续在线阅读_无删减免费完结_
  • 旅游结婚那天未婚夫另娶女秘书,我让他们一无所有连载_老公迎宾超长版_小说后续在线阅读_无删减免费完结_
  • 完结文异界修仙我的直播间能打赏核弹列表_完结文异界修仙我的直播间能打赏核弹(江流年沈红菱)
  • 全书浏览陪弟弟混骑行圈,离婚你哭什么?(韩星河柳千雪)_陪弟弟混骑行圈,离婚你哭什么?(韩星河柳千雪)全书结局
  • 老公出轨我助攻虐心反转_秦绍卿卿阿溪后续加长_小说后续在线阅读_无删减免费完结_
  • 替老婆坐牢出狱那天,我被送去斗兽场精校文本_许言沈宇郑子番茄热门_小说后续在线阅读_无删减免费完结_
  • 沐巍澜温溪:结局+番外(爱如一刹花火)电子书畅享阅读无广告
  • 傅凌川林柚续集(傅凌川林柚)章节前文+全书阅读(替嫁燃婚:小祖宗嘴硬腰软)最新连载
  • 资助生把导盲犬做成火锅后,我家杀疯了!列表_资助生把导盲犬做成火锅后,我家杀疯了!(陈米雪波波)

    关于我们 | 我要投稿 | 免责申明

    Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1