为什么rapidjson在使用std::string时会出现问题?

3

I am trying to use an std::string with RapidJson

using namespace std;
using namespace rapidjson;
const char* json = "{\n"
                   "    \"id\": null\n"
                   "    \"code\": null\n"
                   "}";
Document d;
string a = "myString";
d["myValue"].SetString(a); //error: no matching member function for call to 'SetString' in the compiler

我想通过使用std::string来使用rapidjson编辑我的json文件,但是它不起作用。顺便说一下,我是c++的新手,所以如果这是个愚蠢的问题,请原谅。

编辑:我尝试了Jorge Perez提供的解决方案,但我仍然收到以下错误:

Original Answer翻译成"最初的回答"

/include/rapidjson/document.h:1139: rapidjson::GenericValue<Encoding, Allocator>& rapidjson::GenericValue<Encoding, Allocator>::operator[](const rapidjson::GenericValue<Encoding, SourceAllocator>&) [with SourceAllocator = rapidjson::MemoryPoolAllocator<>; Encoding = rapidjson::UTF8<>; Allocator = rapidjson::MemoryPoolAllocator<>]: Assertion `false' failed.

最初的回答
有什么想法吗?

我有一个可用的 JSON,我可以使用 Document 具有的其他 Set 方法。 - undefined
出了什么问题?无法编译吗?运行时崩溃吗?还是其他原因?请提供一个最小可复现的示例(MCVE)。 - undefined
在编译之前,它就给我报错了。我刚刚编辑了问题并添加了一些代码。 - undefined
个人而言,我会使用nlohmann json库。它更简单、更易于使用。 - undefined
1个回答

1
如果你有一个字符串:
std::string s = "myString"; 

你可以通过使用数据和大小在RapidJson中进行设置:

document["myValue"].SetString(s.data(), s.size(), document.GetAllocator());

我做了那个,现在它给了我ScrabbleClient: ../Downloads/rapidjson-master/include/rapidjson/document.h:1139: rapidjson::GenericValue<Encoding, Allocator>& rapidjson::GenericValue<Encoding, Allocator>::operator[](const rapidjson::GenericValue<Encoding, SourceAllocator>&) [with SourceAllocator = rapidjson::MemoryPoolAllocator<>; Encoding = rapidjson::UTF8<>; Allocator = rapidjson::MemoryPoolAllocator<>]: Assertion `false' failed. 有什么想法吗? - undefined
所以,@IvanSolis,你有没有读过位于./Downloads/rapidjson-master/include/rapidjson/document.h文件中1139行附近的内容?因为在1105行上有一个非常好的注释 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接