JsonCpp 判断 value 中是否有某个KEY

Posted 那一剑的风情

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JsonCpp 判断 value 中是否有某个KEY相关的知识,希望对你有一定的参考价值。

JsonCpp如何判断是否有某个KEY,使用json[“key”]和isXXX的函数即可。
如果json中没有key键,则会创建一个空成员或者返回一个空成员。

bool isNull() const;
bool isBool() const;
bool isInt() const;
bool isUInt() const;
bool isIntegral() const;
bool isDouble() const;
bool isNumeric() const;
bool isString() const;
bool isArray() const;
bool isObject() const;

例子:

ifstream ifs;
ifs.open("testR.json");

Json::Reader reader;
Json::Value value;
if (!reader.parse(ifs,value,false))
{
	return -1;
}

if (value["name"].isString())
{
	string name = value["name"].asString();
	cout << "name:" << name << endl;
}

如果value中没有 name 键就不会提取该数据。

  

以上是关于JsonCpp 判断 value 中是否有某个KEY的主要内容,如果未能解决你的问题,请参考以下文章