我需要在JSON字符串中检索嵌套对象,我正在尝试使用rapidjson.我发现的只是如何检索数组和基本类型,而不是子对象.我创建了以下玩具示例,它给出了一个错误:
rapidjson::Document document;
std::string test = " { \"a\": { \"z\" : 21 } } ";
std::cout << test << std::endl;
if ( document.Parse<0>( test.c_str() ).HasParseError() ) {
std::cout << "Parsing error" << std::endl;
} else {
if ( document[ "a" ].IsObject() ) {
std::cout << "OK" << std::endl;
std::cout << document[ "a" ].GetString() << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
这是执行时的输出:
{ "a": { "z" : 21 } }
OK
JSONTest: ../rapidjson/document.h:441: const typename Encoding::Ch* rapidjson::GenericValue<Encoding, Allocator>::GetString() const [with Encoding = rapidjson::UTF8<char>, …Run Code Online (Sandbox Code Playgroud) 如何将RapidJSON文档序列化为字符串?
在所有示例中,序列化文本通过重定向到标准输出FileStream,但我需要将其重定向到字符串变量.
我是rapidjson的新手.我有test.json哪些包含{"points": [1,2,3,4]}
我使用以下代码来获取数组的数据 "points"
std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename("json/deluxe/treasurebag.json");
unsigned long bufferSize = 0;
const char* mFileData = (const char*)CCFileUtils::sharedFileUtils()->getFileData(fullPath.c_str(), "r", &bufferSize);
std::string clearData(mFileData);
size_t pos = clearData.rfind("}");
clearData = clearData.substr(0, pos+1);
document.Parse<0>(clearData.c_str());
assert(document.HasMember("points"));
const Value& a = document["points"]; // Using a reference for consecutive access is handy and faster.
assert(a.IsArray());
for (SizeType i = 0; i < a.Size(); i++) // rapidjson uses SizeType instead of size_t.
CCLOG("a[%d] = %d\n", i, a[i].GetInt());
Run Code Online (Sandbox Code Playgroud)
结果是
Cocos2d: a[0] = 1
Cocos2d: …Run Code Online (Sandbox Code Playgroud) 我有一个大约36 GB的json文件(来自wikidata),我希望更有效地访问它.目前我在C++中使用rapidjsons SAX风格的API - 但解析整个文件需要我的机器大约7415200毫秒(= 120分钟).我想根据json对象内部的两个主键之一('name'或'entity-key' - >'Stack Overflow'或'Q549037')访问此文件中的json对象.这意味着我必须在最坏的情况下解析当前的整个文件.
所以我想到了两种方法:
ftell()文件中的位置构建某种索引.构建索引应该花费大约120分钟(就像现在解析一样),但访问速度应该更快
std::unorderedmap(可能再次遇到内存问题)这样的问题最好的做法是什么?我应该遵循哪种方法?还有其他想法吗?
我想使用rapidjson创建一个json字符串.但是我收到了一个错误:无法转换std::string为rapidjson::Type.
int x = 111;
string className = "myclass";
Document doc;
auto& allocator = doc.GetAllocator();
doc.AddMember("x", Value().SetInt(x), allocator);
doc.AddMember("className", className, allocator);
unordered_map<string, string>& map = sprite->toMap();
for (const auto& pair : map) {
Value key(pair.first.c_str(), pair.first.size(), allocator);
doc.AddMember(key, pair.second, allocator);
}
StringBuffer sb;
Writer<StringBuffer> writer(sb);
doc.Accept(writer);
log("json string: %s", sb.GetString());
Run Code Online (Sandbox Code Playgroud) 我需要一个有效的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,但错误仍然存在)
我正在阅读RapidJSON的代码,我不明白这段代码:
//! Reserve n characters for writing to a stream.
template<typename Stream>
inline void PutReserve(Stream& stream, size_t count) {
(void)stream;
(void)count;
}
//! Put N copies of a character to a stream.
template<typename Stream, typename Ch>
inline void PutN(Stream& stream, Ch c, size_t n) {
PutReserve(stream, n);// I think this function does nothing
for (size_t i = 0; i < n; i++)
PutUnsafe(stream, c);
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以为我解释'PutReserve'的目的吗?
是否可以尝试在 C++ 中捕获断言调用?我使用库rapidjson(静态库)并且很烦人,因为如果它无法在json文件中找到某些东西,它就会调用assert。当我想避免它调用 assert 并自己处理错误时。
我正在尝试将 Rapidjson::value 复制到类成员中。
\n\nerror: \xe2\x80\x98rapidjson::GenericValue<Encoding, <template-parameter-1-2> >::GenericValue(const rapidjson::GenericValue<Encoding, <template-parameter-1-2> >&) [with Encoding = rapidjson::UTF8<char>; Allocator = rapidjson::MemoryPoolAllocator<rapidjson::CrtAllocator>]\xe2\x80\x99 is private\nRun Code Online (Sandbox Code Playgroud)\n\n只是为了执行以下行:
\n\nvoid setData(const rapidjson::Value json) {\n this->json = json;\n\n}\nRun Code Online (Sandbox Code Playgroud)\n\n知道如何简单地将rapidjson对象复制到类成员中,以便稍后可以对其进行解析吗?
\n我正在 FreeRTOS 中运行资源有限的单线程系统。
我已经为 RapidJSON 分配器预分配了缓冲区,如下所示:
char valueBuffer[2048];
char parseBuffer[1024];
rapidjson::MemoryPoolAllocator<FreeRTOSRapidJSONAllocator> valueAllocator (valueBuffer, sizeof(valueBuffer))
rapidjson::MemoryPoolAllocator<FreeRTOSRapidJSONAllocator> parseAllocator (parseBuffer, sizeof(parseBuffer));
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是,每次使用其中一个分配器时,其大小都会不断增加(并在必要时分配新内存),除非它们被清除。Clear()调用分配器的问题是Malloc,当下次调整分配器大小时,会再次调用分配器,这是我想避免的。
有没有办法简单地重用现有的预分配内存,例如将分配器的大小设置回零?