c++ - Rapidjson 查找成员

标签 c++ rapidjson

我有一个像这样的 JSON 字符串:

{"callCommand":{"command":"car","floor":"2","landing":"front"}}

现在,我想检查是否有名为 command 的名称并获取值。是否可以?我的代码如下,但它不起作用。

const char json[] = "{\"callCommand\":{\"command\":\"car\",\"floor\":\"2\",\"landing\":\"front\"}}";

rapidjson::Value::ConstMemberIterator itr = d.FindMember("command");

if (itr != d.MemberEnd())
    printf("command = %s\n", d["callCommand"]["command"].GetString());

最佳答案

您正在文档的顶层搜索“命令”:

d.FindMember("command");

当您应该在“callCommand”中搜索它时:

d["callCommand"].FindMember("command");

此外,在使用 FindMember 搜索之后,您应该使用迭代器而不是使用 operator[] 再次搜索。像这样的东西:

// assuming that "callCommand" exists
rapidjson::Value& callCommand = d["callCommand"];
rapidjson::Value::ConstMemberIterator itr = callCommand.FindMember("command");

// assuming "command" is a String value
if (itr != callCommand.MemberEnd())
    printf("command = %s\n", itr->value.GetString());

关于c++ - Rapidjson 查找成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30054046/

相关文章:

curl - 快速 JSON 失败,断言 `IsObject()' 失败

c++ - 在 Windows 2000 中使用 Visual Studio 2010 编译的 C++ DLL 时出现问题

c++ - 我正在尝试从 GMocked 类返回一个 rapidjson::Value 但我似乎无法让它工作

c++ - 如何隔离arm设备上的故障?

c++ - 两个具有相同元素的常量缓冲区

c++ - rapidjson:如何将 Document 对象拆分为较小的 Document 对象?

c++ - Rapidjson : Extract a parameter from JsonObject using rapidjson

c++ - 使用 QSortFilterProxyModel 过滤 QTableView 后保留选择

c++ - 构造局部对象——常量引用

java - 使用 JNI 将 Java 与 C++ 静态链接