2015-05-05 42 views
0

我有一个JSON字符串是这样的:Rapidjson findmember

{"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()); 

回答

-1

您可以使用rapidjson的HasMember功能,如下图:

Document doc; 
doc.Parse(json); 
doc.HasMember("command");//true or false 
0

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

d.FindMember("command"); 

当你应该寻找它里面的 “callCommand”:

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

而且,你FindMember搜索后,你应该使用迭代器而不是搜索再次使用操作符[]的。喜欢的东西:

// 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());