如何将 json 文件读入 C++ 字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13935364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:50:02  来源:igfitidea点击:

How to read a json file into a C++ string

c++jsonstringfstream

提问by aiziyuer

My code like this:

我的代码是这样的:

std::istringstream file("res/date.json");
std::ostringstream tmp;
tmp<<file.rdbuf();
std::string s = tmp.str();
std::cout<<s<<std::endl;

The output is res/date.json, while what I really want is the whole content of this json file.

输出是res/date.json,而我真正想要的是这个 json 文件的全部内容。

回答by Bart van Ingen Schenau

This

这个

std::istringstream file("res/date.json");

creates a stream (named file) that reads from the string "res/date.json".

创建一个file从字符串中读取的流(名为)"res/date.json"

This

这个

std::ifstream file("res/date.json");

creates a stream (named file) that reads from the file named res/date.json.

创建一个file从名为 的文件中读取的流(名为)res/date.json

See the difference?

看到不同?

回答by aiziyuer

I found a good solution later. Using parserin fstream.

后来我找到了一个很好的解决方案。使用parserfstream

std::ifstream ifile("res/test.json");
Json::Reader reader;
Json::Value root;
if (ifile != NULL && reader.parse(ifile, root)) {
    const Json::Value arrayDest = root["dest"];
    for (unsigned int i = 0; i < arrayDest.size(); i++) {
        if (!arrayDest[i].isMember("name"))
            continue;
        std::string out;
        out = arrayDest[i]["name"].asString();
        std::cout << out << "\n";
    }
}

回答by PSXGamerPro1

I tried the stuff above but the thing is they dont work in C++ 14 for me :P I get stuff like from ifstream incomplete type is not allowedon both answers AND 2 json11::Json does not have a ::Readeror a ::Valueso Answer 2 does not work either I thin the answoer for ppl who use this https://github.com/dropbox/json11is to do something like this:

我尝试了上面的内容,但问题是它们在 C++ 14 中对我不起作用:P 我从 ifstream 中得到incomplete type is not allowed了两个答案和 2 json11::Json 没有这样的东西::Reader::Value所以答案 2 不起作用或者我瘦了 answoer对于使用这个https://github.com/dropbox/json11 的人来说,是做这样的事情:

ifstream ifile;
int fsize;
char * inBuf;
ifile.open(file, ifstream::in);
ifile.seekg(0, ios::end);
fsize = (int)ifile.tellg();
ifile.seekg(0, ios::beg);
inBuf = new char[fsize];
ifile.read(inBuf, fsize);
string WINDOW_NAMES = string(inBuf);
ifile.close();
delete[] inBuf;
Json my_json = Json::object { { "detectlist", WINDOW_NAMES } };
while(looping == true) {
    for (auto s : Json::array(my_json)) {
        //code here.
    };
};

Note: that is is in a loop as I wanted it to loop the data. Note: there are bound to be some errors with this but at least I opened the file correctly unlike above.

注意:这是在循环中,因为我希望它循环数据。注意:这肯定会有一些错误,但至少我不像上面那样正确地打开了文件。

回答by PlsWork

Load a .jsonfile into an std::stringand write it to the console:

.json文件加载到 anstd::string并将其写入控制台:

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>

int main(int, char**) {

    std::ifstream myFile("res/date.json");
    std::ostringstream tmp;
    tmp << myFile.rdbuf();
    std::string s = tmp.str();
    std::cout << s << std::endl;

    return 0;
}