我很惊讶下面的代码无法按预期工作,我记得我有一个类似的json,其中包含很多数字,但是我不知道它何时停止工作!
std::string_view jsonStr = "{ \"num\": 1, \"str\": 's' }";
rapidjson::Document doc;
doc.Parse(jsonStr.data());
std::cout << "num : " << doc["num"].GetUint() << std::endl;
输出 :
0
if I remove the str
I get 1
!
我做错什么了吗?
那是无效的JSON。
字符串用双引号而不是单引号引起来。
Refer to https://json.org for the schema.
然后在JSON解析库中检查错误代码:它将在某处告诉您,而您忽略了它。
GetUint()
on a non-existent property of a broken document will be returning zero either by default or as part of some "undefined behaviour" in the library; read its documentation to find out which.