【问题标题】:Why is rapidjson giving me problems with std::string?为什么 rapidjson 给我的 std::string 带来问题?
【发布时间】:2019-04-09 05:56:50
【问题描述】:

我正在尝试将 std::string 与 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 的解决方案,但仍然收到此错误:

/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 方法。
  • 不以什么方式工作?不编译?运行时崩溃?还有什么?请提供minimal reproducible example
  • 它在编译之前给了我错误,我只是用更多的代码编辑了这个问题。
  • 就个人而言,我会改用 nlohmann json 库。方式更简单,更易于使用。

标签: c++ json rapidjson


【解决方案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& rapidjson: :GenericValue::operator[](const rapidjson::GenericValue&) [with SourceAllocator = rapidjson::MemoryPoolAllocator;编码 = rapidjson::UTF8;分配器 = rapidjson::MemoryPoolAllocator]:断言“假”失败。有什么想法吗?
  • 所以@IvanSolis 你读过 ./Downloads/rapidjson-master/include/rapidjson/document.h 内 1139 行附近的内容吗?因为在第 1105 行有一个非常好的comment
猜你喜欢
  • 1970-01-01
  • 2022-01-20
  • 2013-07-11
  • 1970-01-01
  • 1970-01-01
  • 2020-05-23
  • 1970-01-01
  • 2022-06-29
相关资源
最近更新 更多