0

我正在尝试使用谷物来反序列化 JSON 消息。(当前)消息结构如下:

"A":"B",
"C":{"D":"E","F":"G","H":"I"},
"J":"K",
"L":"M",
"N":{"O":{"P":"Q"}}}

最重要的数据是“Q”。我可以轻松读取所有正常字符串。我能够轻松阅读顶级项目。我最终能够通过将“C”视为一个数组来读取“D”-“I”(尽管缺少“[”和“]”)。但是,我无法将“N”归档为字符串、数组、向量或对象。它只是使相应的 IsString()、IsObject() 等检查失败iarchive(C);。我看到 a GetType(),但我不知道在 "N" 上调用它,因为我尝试用 "N" 做的任何事情都失败了。

然后我能够将字符串分解为

{"O":{"P":"Q"}}

太好了,所以只是“C”,但更简单。不幸的是,我遇到了和以前一样的问题。

然后我将其分解为 {"P":"Q"} 并最终能够通过以下方式获得 Q 的值iarchive(value)

std::string msgtype,protocol_version, command, radio1_frequency,value;
    std::string unit_id[3];
    std::stringstream ss(msg->get_payload());
    {//at this point, ^ss has  the same format as outlined above, from "A" to "Q."
        cereal::JSONInputArchive iarchive(ss);
        iarchive(CEREAL_NVP(msgtype), CEREAL_NVP(protocol_version), CEREAL_NVP(command),CEREAL_NVP(unit_id));<-----------------------
    }
    rapidjson::Document doc;
    doc.Parse((msg->get_payload()).c_str());
    const rapidjson::Value& vars = doc["variables"];//<--this string is N
    rapidjson::StringBuffer sb;
    rapidjson::Writer<rapidjson::StringBuffer> writer(sb);
    vars.Accept(writer);
    std::stringstream sst(sb.GetString());
    std::cout << sst.str() << "\n"; //here, sst has {"O":{"P":"Q"}}
    doc.Parse(sb.GetString());

    const rapidjson::Value& val = doc["radio1_frequency"];//<--this string is O
    sb.Clear();
    rapidjson::Writer<rapidjson::StringBuffer> writerTwo(sb);
    val.Accept(writerTwo);
    std::cout << sb.GetString() << "\n";//ss3 will now have {"P":"Q"} 
    std::stringstream ss3(sb.GetString());
    {
        cereal::JSONInputArchive iarchive(ss3);
        iarchive((value));
        std::cout << value << '\n';//Q
    }

如果我添加,CEREAL_NVP(variables)到带有箭头的行,我会得到以下输出:

terminate called after throwing an instance of 'cereal::RapidJSONException'
  what():  rapidjson internal assertion failure: IsString()

Child terminated with signal = 0x6 (SIGABRT)

明确地说,我得到了我想要的结果,Q。我只是觉得必须有更好的方法。我觉得问题的根源在于我无法确定 N 是什么类型。由于我不知道类型,我不知道如何正确地将其归档在麦片中。在使用 rapidjson 的 C++11 和谷物领域内,我能做些什么更好?

我想知道是不是因为“O”在字符串中间有一个数字,所以把它扔掉了。我不希望因为这是我相信的有效字符串

编辑:我忘了提到“O”可以改变。所以我将无法对其进行硬编码doc["N"]["O"]["P"]

4

0 回答 0