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中没有 aaa 键就不会提取该数据,aaa会赋初值0,不会报错。
int aaa = value["aaa"].asInt(); 

  

  

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM