gpt4 book ai didi

c++ - Rapidjson 查找成员

转载 作者:太空宇宙 更新时间:2023-11-04 14:31:25 25 4
gpt4 key购买 nike

我有一个像这样的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com