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

然后创建一个Document对象

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

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

  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

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

然后再遍历这个数组

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

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

另外的两个也同理

总的代码如下:

  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. using namespace std;
  13. using namespace rapidjson;
  14. void json_analysis( const string filename, vector<tuple<string, int , int >> &result)
  15. {
  16. ifstream json_file;
  17. json_file.open(filename.c_str());
  18. string json;
  19. if (!json_file.is_open())
  20. {
  21. cout << "Error opening file" << endl;
  22. exit(1);
  23. }
  24. getline(json_file, json);
  25. Document d;
  26. d.Parse<0>(json.c_str());
  27. Value & ques = d[ "questions" ];
  28. string temp_ques;
  29. int temp_ima_id, temp_ques_id;
  30. if (ques.IsArray())
  31. {
  32. for ( size_t i = 0; i < ques.Size(); ++i)
  33. {
  34. Value & v = ques[i];
  35. assert(v.IsObject());
  36. if (v.HasMember( "question" ) && v[ "question" ].IsString()) {
  37. temp_ques = v[ "question" ].GetString();
  38. }
  39. if (v.HasMember( "image_id" ) && v[ "image_id" ].IsInt()) {
  40. temp_ima_id = v[ "image_id" ].GetInt();
  41. }
  42. if (v.HasMember( "question_id" ) && v[ "question_id" ].IsInt()) {
  43. temp_ques_id = v[ "question_id" ].GetInt();
  44. }
  45. auto temp = make_tuple(temp_ques, temp_ima_id, temp_ques_id);
  46. result.push_back(temp);
  47. }
  48. }
  49. /*
  50. for (size_t i = 0; i < result.size(); i++){
  51. cout << get<0>(result[i]) << endl;
  52. cout << get<1>(result[i]) << endl;
  53. cout << get<2>(result[i]) << endl;
  54. }
  55. return 0;
  56. */
  57. }
那么只需要用文件流把这个字符串送到一个string里然后创建一个Document对象再把string转成const char *类型以后送到rapidjson自带的函数Parse里处理就好了我当时遇到的是这样的json字符串:[html] view plain copy{"info": {"description":
R api d JSON 是一个 C++ 的 JSON 解析 器及生成器。它的灵感来自 R api dXml。使用方法:R api d JSON : 首页http://r api d json .org/zh-cn/index.html 这里 r api d json 为作用域; 那么现在该 JSON 就会被 解析 至 中,成为一棵 *DOM 树 *: 让我们查询一下根 Object 中有没有 成员。由于一个 可包含不同类型的值,我们可能需要验证它的类型,并使用合适的 API 去获取其值。在此例中, 成员关联
R api d JSON 是腾讯开源的一个高效的C++ JSON 解析 器及生成器,它是只有头文件的C++库,所以使用cmake非常好用。R api d JSON 是跨平台的,支持Windows, Linux, Mac OS X及iOS, Android。GitHub地址,接口非常简单。 {"info": {"description": "This is v1.0 of the VQA dataset.", "license": {"questions":[ {"question
感谢开源! git上有很多 解析 json 的库文件,比如常见的 Json Cpp、c JSON 等,这些都能满足正常使用。但一旦有了速度、轻量化、简便的追求,就不得不比较一下各个库的优劣了。 荡某乎上的一张各种比较 json 的性能图,可以看出,rapd Json 处于领先地位! 楼主自从体验了r api d Json ,就果断放弃了 Json Cpp,原因也很简单! R api d Json 的github地址: https://github.com/Tencent/r api d json 只有头文件,也只需要包含头文件! json cpp还需要内置
// 请自己下载开源的r api d json #include r api d json /prettywriter.h #include r api d json /r api d json .h #include r api d json /document.h #include r api d json /stringbuffer.h #include r api d json /writer.h #include r api d json /memorystream.h #inclu
在工作中需要在函数里面创建一个 json 的dom 然后将这个数据返回到http客户端,这里分享以下怎么去创建一个dom,以及怎么去项dom中添加一般的成员和 array [ 数组 ]以及object{对象}。 在我的前一篇博客中分享了如何使用r api d json 去分析一个 json 对象的参数,这一篇主要介绍如何去创建一个 json 。 创建一个 json 的第一步是声明一个dom。 然后给dom一个初始要 解析 的string r api d json ::Document document; document.Parse("{
刚开始想用 json cpp的,发现这东西相当不友好,VS2013就是编译不了,于是就弃坑了 发现r api d json 超级好用,只需要包含头文件,也就是可以跨平台 虽然写很复杂的功能的时候可能需要自己封装一些接口,但是写简单的 json 解析 完全够用了 如果是有一个文件,里面有 json 格式的字符串 那么只需要用文件流把这个字符串送到一个string里 然后创建一个Document对象 再把str
//创建纯 数组 r api d json ::Document doc; r api d json ::Document::AllocatorType &allocator = doc.GetAllocator(); r api d json ::Value array 1(r api d json ::Type::k Array Type); r api d json ::StringBuffer s; r api d json ::Wri...
ciscole: 直接登录用户名和密码怎么添加到URL? http://192.168.1.5/axis-cgi/admin/restart.cgi?username="root"&password="password" 这样的格式吗?