我正在尝试从 JSON 文件中读取 while 条件是std::eof
但我得到以下 exption :
在抛出 'nlohmann::detail::parse_error' 的实例后调用终止 what(): [json.exception.parse_error.101] 在第 1 行第 1 列解析错误:解析值时出现语法错误 - 输入意外结束;预期的 '['、'{' 或文字
这是文件:
[{"id":0,"price":0,"qty":0},{"id":1,"price":1,"qty":1}]
这是代码:
#include <iostream>
#include <json.hpp>
#include <fstream>
using json = nlohmann::json;
int main() {
std::ofstream f;
f.open("test.json",std::ios_base::trunc |std::ios_base::out);
json result = json::array();
for(int i =0 ;i < 2 ; i++)
{
json j = {
{"id",i},
{"qty",i},
{"price",i}
};
result.push_back(j);
}
f << result;
f.close();
std::ifstream j;
std::string line;
j.open("test.json",std::ios_base::in);
json ob;
while (!j.eof())
{
j>>ob;
std::cout << ob << "\n";
}
j.close();
return 0;
}