我有一个这样的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());发布于 2017-07-19 06:55:27
您在文档的顶层搜索"command“:
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());发布于 2015-12-15 08:22:41
您可以使用rapidjson的HasMember函数,如下所示:
Document doc;
doc.Parse(json);
doc.HasMember("command");//true or falsehttps://stackoverflow.com/questions/30054046
复制相似问题