C++ abort() 已在 RapidJSON 上调用

c++ abort() has been called on rapidjson

本文关键字:RapidJSON 调用 已在 abort C++      更新时间:2023-10-16

我正在做一些代码,我需要使用 rapidjson 才能获取 json 值

首先我从文件中检索信息

   ifstream  myReadFile;
    myReadFile.open("results.txt");
    string output;
    if (myReadFile.is_open()) {
        while (!myReadFile.eof()) {
            myReadFile >> output;
        }
    }
    myReadFile.close();

结果示例.txt:

[{"ID":1,"Name":"SomeName","Description":"Pub"}]

然后我使用 rapidjson 来过滤信息,

const char * json = output.c_str();
Document document;
document.Parse(json);
cout << document["ID"].GetInt();  //Error on the line
cout << document["Name"].GetString());

但是我收到此错误:调试错误! abort() 已被调用

想法?

谢谢你的时间

您的 json 是一个数组,但您正在尝试解析它,因为它不是!

从 json 字符串中删除方括号,然后您的代码应该可以工作或解析数组:

for (SizeType i = 0; i<document.Size(); i++)
{
    const rapidjson::Value &data_vec = document[i];
    int id = data_vec["ID"].GetInt();
    std::string name = data_vec["Name"].GetString();
}