10

私のコードは次のようになります:

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 ファイルの内容全体です。

4

4 に答える 4

13

これ

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

file文字列から読み取るストリーム(名前付き)を作成します"res/date.json"

これ

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

file。という名前のファイルから読み取るストリーム(という名前の)を作成しますres/date.json

違いを見ます?

于 2012-12-18T14:50:30.670 に答える
4

後で良い解決策を見つけました。で使用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";
    }
}
于 2012-12-19T04:02:40.140 に答える
0

上記のものを試しましたが、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.
    };
};

注:データをループしたかったので、これはループ中です。注:これにはいくつかのエラーがあるはずですが、少なくとも上記とは異なり、ファイルを正しく開きました。

于 2016-05-06T07:26:36.890 に答える