那么只需要用文件流把这个字符串送到一个string里
然后创建一个Document对象
再把string转成const char *类型以后送到rapidjson自带的函数Parse里处理就好了
我当时遇到的是这样的json字符串:
-
{"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();把它抓取出来
另外的两个也同理
总的代码如下:
-
#include <cstdio>
-
#include <string>
-
#include <iostream>
-
#include <fstream>
-
#include <vector>
-
#include <tuple>
-
#include "rapidjson/document.h"
-
#include "rapidjson/prettywriter.h"
-
#include "rapidjson/filereadstream.h"
-
#include "rapidjson/filewritestream.h"
-
#include "rapidjson/stringbuffer.h"
-
-
using
namespace
std;
-
using
namespace
rapidjson;
-
-
void
json_analysis(
const
string filename, vector<tuple<string,
int
,
int
>> &result)
-
{
-
ifstream json_file;
-
json_file.open(filename.c_str());
-
string json;
-
if
(!json_file.is_open())
-
{
-
cout <<
"Error opening file"
<< endl;
-
exit(1);
-
}
-
getline(json_file, json);
-
Document d;
-
d.Parse<0>(json.c_str());
-
Value & ques = d[
"questions"
];
-
string temp_ques;
-
int
temp_ima_id, temp_ques_id;
-
if
(ques.IsArray())
-
{
-
for
(
size_t
i = 0; i < ques.Size(); ++i)
-
{
-
Value & v = ques[i];
-
assert(v.IsObject());
-
if
(v.HasMember(
"question"
) && v[
"question"
].IsString()) {
-
temp_ques = v[
"question"
].GetString();
-
}
-
if
(v.HasMember(
"image_id"
) && v[
"image_id"
].IsInt()) {
-
temp_ima_id = v[
"image_id"
].GetInt();
-
}
-
if
(v.HasMember(
"question_id"
) && v[
"question_id"
].IsInt()) {
-
temp_ques_id = v[
"question_id"
].GetInt();
-
}
-
auto temp = make_tuple(temp_ques, temp_ima_id, temp_ques_id);
-
result.push_back(temp);
-
}
-
}
-
-
-
-
-
-
-
-
-
}
那么只需要用文件流把这个字符串送到一个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: