I'm doing some code where i have i need to use rapidjson in order to get the json values
First I retrieve the info from the file
ifstream myReadFile;
myReadFile.open("results.txt");
string output;
if (myReadFile.is_open()) {
while (!myReadFile.eof()) {
myReadFile >> output;
}
}
myReadFile.close();
Example of results.txt:
[{"ID":1,"Name":"SomeName","Description":"Pub"}]
and then I use rapidjson to filter the information,
const char * json = output.c_str();
Document document;
document.Parse(json);
cout << document["ID"].GetInt(); //Error on the line
cout << document["Name"].GetString());
But i get this error: Debug Error! abort() has been called
Ideas?
Thank you for your time
Your json is an array but you are trying to parse it as it was not!
Either remove square brackets from the json string and then your code should work or parse the array:
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();
}