我有一个项目,我在其中导入 JSON 文件来设置全局变量。文件中的 JSON 对象名称有多种可能性,所以我想知道如何检查对象是否存在。我尝试这样做的方式(使用如下所示的 if 语句)导致错误
The program '..\..\build\jacky.exe' has exited with code 3 (0x00000003).
所以它根本不起作用。有没有办法做到这一点?谢谢!
#include <iostream>
#include <string>
#include <fstream>
#include "json.hpp" // json library by nlohmann
std::ifstream gJsonFile;
nlohmann::json j;
int var;
int main(int argc, char *argv[])
{
gJsonFile.open(/* whatever json file path */, std::ifstream::in);
if (gJsonFile.is_open())
{
j << gJsonFile;
if (j["Shirt Size"]) // causes exit with code 3 if false
var = (j["Shirt Size"]);
else
var = (j["AB Circumference"]);
}
}