私のコードは次のようになります:
std::istringstream file("res/date.json");
std::ostringstream tmp;
tmp<<file.rdbuf();
std::string s = tmp.str();
std::cout<<s<<std::endl;
出力はres/date.json
ですが、私が本当に欲しいのは、この json ファイルの内容全体です。
これ
std::istringstream file("res/date.json");
file
文字列から読み取るストリーム(名前付き)を作成します"res/date.json"
。
これ
std::ifstream file("res/date.json");
file
。という名前のファイルから読み取るストリーム(という名前の)を作成しますres/date.json
。
違いを見ます?
後で良い解決策を見つけました。で使用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";
}
}
上記のものを試しましたが、C++ 14では機能しません:Pincomplete type is not allowed
両方の回答でifstreamからのようなものを取得しますAND 2 json11::Jsonにはa::Reader
またはa::Value
がないため、回答2も機能しません。この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.
};
};
注:データをループしたかったので、これはループ中です。注:これにはいくつかのエラーがあるはずですが、少なくとも上記とは異なり、ファイルを正しく開きました。