2017-10-29 2 views
0

私は、.jsonファイルからデータを格納し、データを文字列に変換するjsonオブジェクトを取得しようとしています。私はnolhmann json辞書で作業しています。nolhmann jsonオブジェクトを文字列に分割する方法

はここ.jsonファイルされる:

"dictionary": [ 
    {"word": "MEAGRE", "definition": "A large European scinoid fish (Scina umbra or S. aquila),having white bloodless flesh. It is valued as a food fish. [Writtenalso maigre.]"}, 
    {"word": "GRUGRU WORM", "definition": "The larva or grub of a large South American beetle (Calandrapalmarum), which lives in the pith of palm trees and sugar cane. Itis eaten by the natives, and esteemed a delicacy."} 

ここでは、ファイルのための私のコード

string filename = line.substr(input.size()+1, line.size()); 
    filename[11] = toupper(filename[11]); 
    cout << filename << "\n"; 


    ifstream i(filename); 
    if (i.is_open()) { 
     cout << "it is open\n"; 
     json j; 
     i >> j; 
     for (json::iterator it = j.begin(); it != j.end(); ++it) 
     { 
      cout << *it << "\n"; 
      //how to seperate into strings?? 
     } 

    } 

ですこれはJSONファイルで作業する私の最初の時間ですので、私はまだそれらを理解するためにtryngています。 ので、別々に情報を保存する方法はありますので、異なるJSON辞書の全てが異なる持っているので、私は

文字列=単語の情報

文字列=定義

の情報を持つことができます私はこれを理解することが非常に難しいと思っています。

+0

JSONを自分で解析しないでください。ライブラリを探してください(ここでは質問してはいけない、それはトピックではありません)。 –

+0

@Someprogrammerdudeはライブラリのnlohmannですか? – upsidedownturtle

答えて

0

EDIT - 実際のファイルJSONに基づく実際の例です。注 - 投稿されたJSONは無効なので、最初に修正する必要があります。私は実際のファイルの読み込み部分をスキップし、静的const変数 'json_data_from_file'で示される文字列にファイルが既に読み込まれているかのように、まっすぐにジャンプしました。ファイルをstd :: stringに読み込む際に助けが必要な場合は、私に知らせてください。

#include <iostream> 
#include <string> 
#include "json.h" 

static const std::string json_data_from_file = R"({ 
"dictionary": [{ 
       "word": "MEAGRE", 
       "definition": "A large European scinoid fish (Scina umbra or S. aquila),having white bloodless flesh. It is valued as a food fish. [Writtenalso maigre.]" 
       }, 
       { 
       "word": "GRUGRU WORM", 
       "definition": "The larva or grub of a large South American beetle (Calandrapalmarum), which lives in the pith of palm trees and sugar cane. Itis eaten by the natives, and esteemed a delicacy." 
       } 
       ] 
})"; 

int main(int argc, const char * argv[]) { 
    std::cout << "json data:\n" << json_data_from_file << "\n"; 

    try 
    { 
     // Now attempt to convert file data to json 
     json jdict = json::parse(json_data_from_file); 
     json j = json::parse(jdict["dictionary"].dump()); 
     for (const auto& i : j) { 
      json jsonobj(i); 
      std::string word = jsonobj["word"].dump(); 
      std::cout << "word: " << word << "\n"; 
      std::string definition = jsonobj["definition"].dump(); 
      std::cout << "definition: " << definition << "\n"; 
     } 
    } 
    catch (const std::exception& e) { 
     //... 
    } 
    catch (...) 
    { 
     //... 
    } 

    return 0; 
} 
+0

ss.str()は何ですか? – upsidedownturtle

+0

ファイルデータはすでにjson形式です。データを単語と定義の文字列に分離しようとしています。 – upsidedownturtle

+0

ss.str()は文字列ストリームからstd :: stringを取得します。はい。ファイルデータはjson形式であると予想されます。そうでないと、例外が発生する可能性があります。明確にするために、ss(文字列ストリーム)はファイルデータ(バイト)を保持します。これはjson :: parse()に渡され、jsonオブジェクトに変換されます。その後、辞書として使用することができます。 – kvr

関連する問題