如何将 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
How to read a json file into a C++ string
提问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 parser
in fstream
.
后来我找到了一个很好的解决方案。使用parser
在fstream
。
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 allowed
on both answers AND 2 json11::Json does not have a ::Reader
or a ::Value
so 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 .json
file into an std::string
and 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;
}