断言 for rapidJson 失败了

assert is failing for rapidJson

本文关键字:失败 rapidJson for 断言      更新时间:2023-10-16

我正在尝试使用RapidJson解析c ++中的json数据。我不知道我哪里做错了,但我的断言失败了。当我尝试调试它的显示 sigabrt 时,当它运行行断言时。社区 我感谢您的见解。感谢您回答这个幼稚的问题。

#include <iostream>
#include <assert.h>
#include "hpdf.h"
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include <rapidjson/istreamwrapper.h>
#include <fstream>
int main() {
    std::ifstream ifs("/home/is/..../test.json");
    rapidjson::IStreamWrapper isw(ifs);
    rapidjson::Document document;
    document.ParseStream(isw);

    assert(document.IsObject());
    rapidjson::Value::MemberIterator hello = document.FindMember("timeStamp");
    std::string vali = document["timestamp"].GetString();
    std::cout << vali << std::endl;
    return 0;
}

我也尝试使用 rapidJson 文件流,但在同一行再次失败。

[
  {
    "timeStamp": "...",
    "alertType": "...",
    "instanceId": 8
   }
  ]

阅读有关 JSON 的更多信息。

您的输入文档(以 [ 开头)是一个包含单个 JSON 对象的 JSON 数组。

我建议使用 document.GetType()然后对其结果进行不同的处理(可能带有 switch )。

所以document.IsObject()是假的很正常。对于您的输入document.IsArray()应该是真的,并且该数组中的 JSON 对象可以通过 document[0]<</p>

div class="answers> 99% 访问该数组中的 JSON

是错误的。

然而,"timeStamp"并不等同于"timestamp"

伙计们,我想我已经解决了这个问题。你们从一开始就是对的。我的 data.json 有问题。当 .json 文件中没有 [] 时,此库有效。我删除了方括号,一切似乎都很好。感谢您的时间和见解。我感谢你们的所有答复。