rapidjson解析数组过程

那么只需要用文件流把这个字符串送到一个string里

然后创建一个Document对象

再把string转成const char *类型以后送到rapidjson自带的函数Parse里处理就好了

我当时遇到的是这样的json字符串:

[html]  view plain  copy
  1. {"info": {"description": "This is v1.0 of the VQA dataset.", "url": "http://visualqa.org", "version": "1.0", "year": 2015, "contributor": "VQA Team", "date_created": "2015-10-02 19:35:04"}, "task_type": "Open-Ended", "data_type": "mscoco", "license": {"url": "http://creativecommons.org/licenses/by/4.0/", "name": "Creative Commons Attribution 4.0 International License"}, "data_subtype": "val2014", "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}, {"question": "What has been upcycled to make lights?", "image_id": 350623, "question_id": 3506231}, {"question": "Is there water in the water bottle?", "image_id": 552610, "question_id": 5526102}]}  

可以发现"questions"这个标签下其实是一个数组

所以就先利用

[cpp]  view plain  copy
  1. Value & ques = d["questions"];  

把questions标签下的内容都放到一个Value对象里面

可以利用rapidjson自带的ques.IsArray()来检测是不是数组

然后再遍历这个数组

利用v.HasMember("question")来检测是否带有这个标签,如果带有这个标签的话再利用v["question"].IsString()来检测是否是这个类型

都符合的话就用temp_ques = v["question"].GetString();把它抓取出来

另外的两个也同理

总的代码如下:

[cpp]  view plain  copy
  1. #include <cstdio>  
  2. #include <string>  
  3. #include <iostream>  
  4. #include <fstream>  
  5. #include <vector>  
  6. #include <tuple>  
  7. #include "rapidjson/document.h"  
  8. #include "rapidjson/prettywriter.h"  
  9. #include "rapidjson/filereadstream.h"  
  10. #include "rapidjson/filewritestream.h"  
  11. #include "rapidjson/stringbuffer.h"  
  12.   
  13. using namespace std;  
  14. using namespace rapidjson;  
  15.   
  16. void json_analysis(const string filename, vector<tuple<string, intint>> &result)  
  17. {   
  18.     ifstream json_file;  
  19.     json_file.open(filename.c_str());  
  20.     string json;  
  21.     if (!json_file.is_open())  
  22.     {  
  23.         cout << "Error opening file" << endl;  
  24.         exit(1);  
  25.     }  
  26.     getline(json_file, json);  
  27.     Document d;  
  28.     d.Parse<0>(json.c_str());  
  29.     Value & ques = d["questions"];  
  30.     string temp_ques;  
  31.     int temp_ima_id, temp_ques_id;  
  32.     if (ques.IsArray())  
  33.     {  
  34.         for (size_t i = 0; i < ques.Size(); ++i)  
  35.         {  
  36.             Value & v = ques[i];  
  37.             assert(v.IsObject());  
  38.             if (v.HasMember("question") && v["question"].IsString()) {  
  39.                 temp_ques = v["question"].GetString();  
  40.             }  
  41.             if (v.HasMember("image_id") && v["image_id"].IsInt()) {  
  42.                 temp_ima_id = v["image_id"].GetInt();  
  43.             }  
  44.             if (v.HasMember("question_id") && v["question_id"].IsInt()) {  
  45.                 temp_ques_id = v["question_id"].GetInt();  
  46.             }  
  47.             auto temp = make_tuple(temp_ques, temp_ima_id, temp_ques_id);  
  48.             result.push_back(temp);  
  49.         }  
  50.     }  
  51.     /* 
  52.     for (size_t i = 0; i < result.size(); i++){ 
  53.         cout << get<0>(result[i]) << endl; 
  54.         cout << get<1>(result[i]) << endl; 
  55.         cout << get<2>(result[i]) << endl; 
  56.     } 
  57.     return 0; 
  58.     */  
  59. }  
  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值