我正在尝试解析 Json 文件并将数据存储到二维数组或 vector 中。 Json 文件如下所示:
{"n" : 2,
"x" : [[1,2],
[0,4]]}
这就是我的代码的样子,但我不断收到“json.exception.parse_error.101”错误
#include <iostream>
#include "json.hpp"
#include <fstream>
using json = nlohmann::json;
using namespace std;
int main(int argc, const char * argv[]) {
ifstream i("trivial.json");
json j;
i >> j;
return 0;
}
最佳答案
简而言之,您需要在处理之前进行检查,如下所示:
ifstream i("trivial.json");
if (i.good()) {
json j;
try {
i >> j;
}
catch (const std::exception& e) {
//error, log or take some error handling
return 1;
}
if (!j.empty()) {
// make further processing
}
}
关于c++ - C++ 的 JSON 解析器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57964785/