RapidJSON解析数组

一 简介

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 `····

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在rapidjson中,处理数组中嵌套数组的情况可以使用递归的方式来实现。具体来说,可以先判断当前元素是否为数组,如果是,则遍历该数组的每一个元素,对于每个元素再进行递归处理,直到遍历完所有嵌套的数组。 以下是一个示例代码,假设我们有一个JSON字符串,其中包含嵌套的数组: ```json { "data": [ { "name": "Alice", "scores": [90, 80, 95] }, { "name": "Bob", "scores": [85, 75, [90, 95]] } ] } ``` 我们可以使用以下代码来解析JSON字符串: ```c++ #include <iostream> #include "rapidjson/document.h" #include "rapidjson/writer.h" #include "rapidjson/stringbuffer.h" using namespace rapidjson; void handleArray(const Value& arr) { if (arr.IsArray()) { for (SizeType i = 0; i < arr.Size(); i++) { const Value& v = arr[i]; if (v.IsArray()) { handleArray(v); } else { std::cout << v.GetString() << std::endl; } } } } int main() { const char* json = "{\"data\":[{\"name\":\"Alice\",\"scores\":[90,80,95]},{\"name\":\"Bob\",\"scores\":[85,75,[90,95]]}]}"; Document d; d.Parse(json); const Value& data = d["data"]; handleArray(data); return 0; } ``` 运行以上代码,输出结果为: ``` Alice 90 80 95 Bob 85 75 90 95 ``` 可以看到,我们成功地处理了嵌套的数组。在处理数组时,我们首先判断当前元素是否为数组,如果是,则遍历该数组的每一个元素,对于每个元素再进行递归处理,直到遍历完所有嵌套的数组。如果当前元素不是数组,则直接输出其值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值