小编dai*_*sby的帖子

nlohmann json:为什么我收到解析错误?

我真的很困惑为什么当我尝试使用 nlohmann 库解析 C++ 中的文件时会出现解析错误。

我的代码与我从他们的 GitHub 页面复制的代码完全相同:

using json = nlohmann::json;

int main() {
    ifstream ifs("test.json");

    if (nlohmann::json::accept(ifs))
        cout << "accepted" << endl;
    else
        cout << "not accepted" << endl;

    json j = json::parse(ifs);
}
Run Code Online (Sandbox Code Playgroud)

JSON 文件就是这样:

{ 
   "test": 5
}
Run Code Online (Sandbox Code Playgroud)

由于某种原因,当我到达该parse()函数时,这会引发错误,尽管accept()返回 true,这意味着 JSON 被接受为有效,但当

另外,当我做这样的事情时:

json j = json::parse(R"({"test": "aaa"})");
Run Code Online (Sandbox Code Playgroud)

效果很好。但我无法解析ifstream对象。

有谁知道这里可能存在什么问题吗?

我没有任何想法,我也毫无头绪,因为看起来我做的一切都是对的。

c++ json nlohmann-json

2
推荐指数
1
解决办法
298
查看次数

标签 统计

c++ ×1

json ×1

nlohmann-json ×1