mor*_*rde 7 c++ templates rapidjson
我需要一个有效的c ++代码,用于使用rapidjson从文件中读取文档:https://code.google.com/p/rapidjson/
在wiki中它尚未记录,示例仅从std :: string反序列化,我对模板没有深入的了解.
我将我的文档序列化为文本文件,这是我编写的代码,但它不编译:
#include "rapidjson/prettywriter.h" // for stringify JSON
#include "rapidjson/writer.h" // for stringify JSON
#include "rapidjson/filestream.h" // wrapper of C stream for prettywriter as output
[...]
std::ifstream myfile ("c:\\statdata.txt");
rapidjson::Document document;
document.ParseStream<0>(myfile);
Run Code Online (Sandbox Code Playgroud)
编译错误状态: 错误:'Document'不是'rapidjson'的成员
我正在使用Qt 4.8.1和mingw以及rapidjson v 0.1(我已经尝试升级v 0.11,但错误仍然存在)
Dre*_*kes 14
将FileStream在@ Raanan的回答显然是否决.源代码中有一条评论说要使用FileReadStream.
#include <rapidjson/document.h>
#include <rapidjson/filereadstream.h>
using namespace rapidjson;
// ...
FILE* pFile = fopen(fileName.c_str(), "rb");
char buffer[65536];
FileReadStream is(pFile, buffer, sizeof(buffer));
Document document;
document.ParseStream<0, UTF8<>, FileReadStream>(is);
Run Code Online (Sandbox Code Playgroud)
And*_*kur 11
#include <rapidjson/document.h>
#include <rapidjson/istreamwrapper.h>
#include <fstream>
using namespace rapidjson;
using namespace std;
ifstream ifs("test.json");
IStreamWrapper isw(ifs);
Document d;
d.ParseStream(isw);
Run Code Online (Sandbox Code Playgroud)
请阅读http://rapidjson.org/md_doc_stream.html中的文档.
在遇到类似的问题之后才发现这个问题.解决方案是使用FILE*对象,而不是ifstream和rapidjson自己的FileStream对象(你已经包含了正确的头)
FILE * pFile = fopen ("test.json" , "r");
rapidjson::FileStream is(pFile);
rapidjson::Document document;
document.ParseStream<0>(is);
Run Code Online (Sandbox Code Playgroud)
你当然需要添加document.h include(这可以回答你的直接问题,但是在你的情况下不能解决问题,因为你使用了错误的文件流):
#include "rapidjson/document.h"
Run Code Online (Sandbox Code Playgroud)
然后文档对象(我可能会加快)填充文件内容.希望能帮助到你!