0

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

marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388
Tazz
  • 59
  • 10
  • http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – melpomene Jan 15 '17 at 03:10
  • After i saw that post i tried those lines: "while(myReadFile >> output){" and "while (!(myReadFile>>std::ws).eof()) {" , none seems to work @melpomene – Tazz Jan 15 '17 at 03:28

1 Answers1

0

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();
}
doqtor
  • 8,058
  • 2
  • 15
  • 30
  • Thanks for your time and your explanation, your answer resolved my problem, so i'll accept as the correct answer! :) – Tazz Jan 15 '17 at 14:21