Ошибка синтаксического анализа JSON в C ++ ... в Json :: Value :: getMemberNames () значение должно быть ошибкой objectValue - PullRequest
0 голосов
/ 14 марта 2019

Я пытаюсь разобрать файл json ниже:


    [
      {
        "date": "Mon 24 Dec 7:47:37 2018",
        "toRecipient": "Jai",
        "subject": "Hi Jai",
        "body": "This is Tony",
        "fromRecipient": "Tony"
      },

      {
        "date": "Mon 24 Dec 7:47:38 2018",
        "toRecipient": "Jai",
        "subject": "Hi Jai 2",
        "body": "This is Tony 2",
        "fromRecipient": "Tony"
      },
    etc...

И это моя функция для разбора:

            MessageLibrary::MessageLibrary(string jsonFileName) {
            Json::Reader reader;
            Json::Value root;
            std::ifstream json(jsonFileName.c_str(), std::ifstream::binary);
            bool parseSuccess = reader.parse(json, root, false);

            if (parseSuccess) {
                Json::Value::Members mbr = root.getMemberNames();
                for(vector<string>::const_iterator i = mbr.begin(); i!= mbr.end(); i++) {
                    Json::Value jsonMessage = root[*i];

                    string date = jsonMessage["date"].asString();
                    string toRecipient = jsonMessage["toRecipient"].asString();
                    string subject = jsonMessage["subject"].asString();
                    string body = jsonMessage["body"].asString();
                    string fromRecipient = jsonMessage["fromRecipient"].asString();

                    // create Message objects from parse
                    Message *message = new Message(toRecipient, fromRecipient, subject, body, date);
                    messages[*i] = *message;
}

Кажется, что появляется ошибка, когда я звоню Json::Value::Members mbr = root.getMemberNames(); и я не уверен, как обойти это.

прекращение вызова после броска экземпляра 'Json :: LogicError'
what (): в Json :: Value :: getMemberNames(), значение должно быть objectValue Прервано (ядро сброшено)

1 Ответ

0 голосов
/ 16 марта 2019

Благодаря пользователю xyz347 я смог реализовать весь мой JSON-массив и нуждался в том, чтобы проходить по нему по-другому, например:


    if (parseSuccess) {
            for(Json::Value::ArrayIndex i = 0; i != root.size(); i++) {

                string date = root[i]["date"].asString();
                string toRecipient = root[i]["toRecipient"].asString();
                string subject = root[i]["subject"].asString();
                string body = root[i]["body"].asString();
                string fromRecipient = root[i]["fromRecipient"].asString();

                // create Message objects from parse
                Message *message = new Message(toRecipient, fromRecipient, subject, body, date);
                // messages[*i] = *message;    // this doesn't work though
            }

...