简体   繁体   English

c++程序中使用nlohmann解析JSON文件

[英]Using nlohmann to parse a JSON file in a c++ program

I have a very basic function below that I am trying to use in hopes of parsing a simple JSON object from a file.我在下面有一个非常基本的 function,我正在尝试使用它,希望从文件中解析一个简单的 JSON object。 I have looked at similar questions that get the same [json.exception.parse_error.101 which all point to the fact that the file is not being opened, however from my understanding that does not seem to be the issue here.我看过类似的问题,得到相同的[json.exception.parse_error.101都指向文件未打开的事实,但据我了解,这似乎不是这里的问题。 I am able to print out the buffer stream which matches my JSON file exactly, so I know it is finding the file and extracting its contents.我能够打印出与我的 JSON 文件完全匹配的缓冲区 stream,所以我知道它正在查找文件并提取其内容。 I am new to using the nlohmann library and am truly at a loss for why the program keeps throwing this error.我是使用nlohmann库的新手,我真的不知道为什么程序会不断抛出这个错误。 I would greatly appreciate any pointers as to why this might be happening.我将不胜感激任何关于为什么会发生这种情况的指示。

.cpp file .cpp 文件

#include <nlohmann/json.hpp>
#include <fstream>
#include <iostream>
 
int main(int argc, char *argv[]) {
    std::ifstream myfile("test.json");
    if (myfile.is_open()) {
 
        std::cout << myfile.rdbuf() << std::endl;
        nlohmann::json data = nlohmann::json::parse(myfile);
    }
    return 0;
}

.json file .json 文件

{
    "firstName" : "Jane",
    "lastName" : "Doe",
    "age" : 100
}

print statement/ error message打印语句/错误信息

{
    "firstName" : "Jane",
    "lastName" : "Doe",
    "age" : 100
}
terminate called after throwing an instance of 'nlohmann::detail::parse_error'
  what():  [json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected end of input; expected '[', '{', or a literal

other solutions其他解决方案

I have also tried using an older method to store the JSON object in my data variable using this operator myfile >> data , but the same error is thrown.我还尝试使用旧方法使用此运算符将 JSON object 存储在我的data变量中myfile >> data ,但抛出相同的错误。

std::cout << myfile.rdbuf() reads the entire file, and sends it to std::cout . std::cout << myfile.rdbuf()读取整个文件,并将其发送到std::cout Once that's done, there's no more data left to read, so when you pass myfile to json::parse , that function immediately gets an end of file indication.一旦完成,就没有更多数据可供读取,因此当您将myfile传递给json::parse时,function 会立即获得文件结束指示。

If you want to both print the file and parse it, you'll need to rewind the file (seek to 0) or reopen it after you print it.如果既要打印文件又要解析文件,则需要倒带文件(查找到 0)或在打印后重新打开文件。

I don't know if this has anything to do with your original problem.我不知道这是否与您的原始问题有关。 If you haven't yet done this, try calling json::parse before you print the file (which will mean that the EOF will be received by std::cout << myfile.rdbuf() instead of json::parse ; it still won't both print and parse the file).如果您还没有这样做,请尝试在打印文件之前调用json::parse (这意味着 EOF 将由std::cout << myfile.rdbuf()而不是json::parse ;它仍然不会同时打印和解析文件)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM