📢 Webサイト閉鎖と移転のお知らせ
このWebサイトは2026年9月に閉鎖いたします。
新しい記事は移転先で追加しております。(旧サイトでは記事を追加しておりません)

文字列「__FORCETOC__」を「{{#seo: |title={{PAGENAME}} : Exploring Electronics and SUSE Linux | MochiuWiki |keywords=MochiuWiki,Mochiu,Wiki,Mochiu Wiki,Electric Circuit,Electric,pcb,Mathematics,AVR,TI,STMicro,AVR,ATmega,MSP430,STM,Arduino,Xilinx,FPGA,Verilog,HDL,PinePhone,Pine Phone,Raspberry,Raspberry Pi,C,C++,C#,Qt,Qml,MFC,Shell,Bash,Zsh,Fish,SUSE,SLE,Suse Enterprise,Suse Linux,openSUSE,open SUSE,Leap,Linux,uCLnux,Podman,電気回路,電子回路,基板,プリント基板 |description={{PAGENAME}} - 電子回路とSUSE Linuxに関する情報 | This pag…
62行目: 62行目:
<br>
<br>


==== 使用方法 ====
==== JSONファイルの読み込み ====
  <syntaxhighlight lang="c++">
  <syntaxhighlight lang="c++">
  #include <iostream>
  #include <iostream>
68行目: 68行目:
  #include "nlohmann/json.hpp"
  #include "nlohmann/json.hpp"
   
   
  int main()
  // JSONファイルの読み込み
{
string filename = "JSON-Sample.json";
    // JSONファイルの内容
  ifstream ifs(filename.c_str());
    json m_json = {
                  {"FirstName", "Jung kook"},
                  {"LastName", "Park"},
                  {"Country",  "Korea"},
                  {"Age",      25},
                  {"Height"    180.5f}
                  };
   
   
    // JSONファイルの名前
  if (ifs.good()) {
    string filename = "JSON-Sample";
     json m_json;
     string Extension = ".json";
     ifs >> m_json
     filename += Extension;
   
   
     // JSONファイルへ書き込み
     // 読み込んだデータを各変数へ代入
     ofstream writing_file;
     string firstname = m_json["FirstName"];
     writing_file.open(filename, ios::out); // JSONファイルの作成またはオープン
     string lastname  = m_json["LastName"];
     writing_file << m_json.dump() << endl; // JSON型のオブジェクトを書き込むには、シリアライズを行う必要がある
     int    age      = m_json["Age"];
     writing_file.close();                   // JSONファイルを閉じる
     double height    = m_json["Height"];
   
   
     // JSONファイルの読み込み
     // 各データの表示
     string filename = "JSON-Sample.json";
     cout << "firstname:" << firstname << endl;
     ifstream ifs(filename.c_str());
     cout << "lastname:" << lastname << endl;
     if (ifs.good())
     cout << "age:" << age << endl;
     {
     cout << "height:" << height << endl;
      json m_json;
}
      ifs >> m_json
else {
    // JSONファイルの読み込みに失敗した場合
    cout << "ファイルの読み込みに失敗しました" << endl;
}
</syntaxhighlight>
<br>
==== JSONファイルの書き込み ====
<syntaxhighlight lang="c++">
#include <iostream>
#include <fstream>
#include "nlohmann/json.hpp"
   
   
      // 読み込んだデータを各変数へ代入
// JSONファイルの内容
      string firstname = m_json["FirstName"];
json m_json = {
      string lastname = m_json["LastName"];
    {"FirstName", "Jung kook"},
      int    age      = m_json["Age"];
    {"LastName", "Park"},
      double height    = m_json["Height"];
    {"Country",  "Korea"},
    {"Age",      25},
    {"Height"     180.5f}
};
   
   
      // 各データの表示
// JSONファイルのパス
      cout << "firstname:" << firstname << endl;
  string filename  = "JSON-Sample.json";
      cout << "lastname:" << lastname << endl;
      cout << "age:" << age << endl;
      cout << "height:" << height << endl;
    }
    else
    { // JSONファイルの読み込みに失敗した場合
      cout << "ファイルの読み込みに失敗しました" << endl;
    }
   
   
    return 0;
// JSONファイルへ書き込み
  }
ofstream writing_file;
writing_file.open(filename, ios::out);  // JSONファイルの作成またはオープン
writing_file << m_json.dump() << endl; // JSON型のオブジェクトを書き込むには、シリアライズを行う必要がある
  writing_file.close();                  // JSONファイルを閉じる
  </syntaxhighlight>
  </syntaxhighlight>
<br><br>
<br><br>