📢 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…
編集の要約なし
 
(同じ利用者による、間の8版が非表示)
1行目: 1行目:
== 概要 ==
== 概要 ==
 
JSON (JavaScript Object Notation) は、軽量なデータ交換フォーマットである。<br>
人間にとって読み書きが容易で、コンピュータにとっても解析や生成が容易なテキストベースのデータ形式である。<br>
<br>
JSONは、元々JavaScriptのオブジェクト表記法から派生したが、現在では言語に依存しないデータ形式として広く使用されている。<br>
多くのプログラミング言語がJSONの解析と生成をサポートしており、Web APIやアプリケーション間のデータ交換、設定ファイル等、様々な用途で利用されている。<br>
<br>
JSONの特徴を以下に示す。<br>
* XMLと比較して軽量で記述が簡潔である。
* Web APIのレスポンス形式として標準的に使用されている。
* 設定ファイルやデータストレージとしても広く利用されている。
<br>
<center>
{| class="wikitable"
|+ JSONのデータ型
|-
! データ型 !! 説明 !! 例
|-
| オブジェクト || キーと値のペアの集合 || <code>{"name": "John", "age": 30}</code>
|-
| 配列 || 値の順序付きリスト || <code>[1, 2, 3, "test"]</code>
|-
| 文字列 || ダブルクォーテーションで囲まれたテキスト || <code>"Hello"</code>
|-
| 数値 || 整数または浮動小数点数 || <code>123</code>, <code>45.67</code>
|-
| ブール値 || 真偽値 || <code>true</code>または<code>false</code>
|-
| null || null値 || <code>null</code>
|}
</center>
<br><br>
<br><br>


36行目: 65行目:
*: <code>std::vector</code>クラスや<code>std::map</code>クラスの使用方法を知っていれば、すぐに使用することができる。
*: <code>std::vector</code>クラスや<code>std::map</code>クラスの使用方法を知っていれば、すぐに使用することができる。
<br>
<br>
<u>JSON for Modern C++は、MITライセンスである。</u><br>
<u>JSON for Modern C++は、[[その他_-_ソフトウェアライセンス#MIT_License|MITライセンス]]である。</u><br>
<br>
<br>
==== JSON for Modern C++のインストール ====
==== JSON for Modern C++のインストール ====
56行目: 85行目:
===== Windows =====
===== Windows =====
[https://github.com/nlohmann/json JSON for Modern C++のGithub]にアクセスして、ソースコードをダウンロードする。<br>
[https://github.com/nlohmann/json JSON for Modern C++のGithub]にアクセスして、ソースコードをダウンロードする。<br>
ダウンロードするファイルは、<u>include.zip</u>または<u>json.hpp</u>と記載されているリンクである。<br>
ダウンロードするファイルは、<u>include.zip</u> または <u>json.hpp</u> と記載されているリンクである。<br>
<br>
<br>
ダウンロードしたファイルを解凍する。<br>
ダウンロードしたファイルを解凍する。<br>
各プロジェクトファイルに配置して、json.hppをインクルードして使用する。<br>
各プロジェクトファイルに配置して、json.hppをインクルードして使用する。<br>
<br>
==== JSONファイルの読み込み ====
<syntaxhighlight lang="c++">
#include <iostream>
#include <fstream>
#include "nlohmann/json.hpp"
using namaspace std;
// JSONファイルの読み込み
string filename = "JSON-Sample.json";
ifstream ifs(filename.c_str());
if (ifs.good()) {
    json m_json;
    ifs >> m_json
    // 読み込んだデータを各変数へ代入
    string firstname = m_json["FirstName"];
    string lastname  = m_json["LastName"];
    int    age      = m_json["Age"];
    double height    = m_json["Height"];
    // 各データの表示
    cout << "firstname:" << firstname << endl;
    cout << "lastname:"  << lastname << endl;
    cout << "age:"      << age << endl;
    cout << "height:"    << height << endl;
}
else {
    // JSONファイルの読み込みに失敗した場合
    cout << "ファイルの読み込みに失敗しました" << endl;
}
</syntaxhighlight>
<br>
<br>


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