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

編集の要約なし
 
(同じ利用者による、間の3版が非表示)
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>


38行目: 67行目:
<u>JSON for Modern C++は、[[その他_-_ソフトウェアライセンス#MIT_License|MITライセンス]]である。</u><br>
<u>JSON for Modern C++は、[[その他_-_ソフトウェアライセンス#MIT_License|MITライセンス]]である。</u><br>
<br>
<br>
==== JSON for Modern C++のインストール ====
==== JSON for Modern C++のインストール ====
===== Linux =====
===== Linux =====
57行目: 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>
<br>
==== JSONファイルの読み込み ====
==== JSONファイルの読み込み ====
  <syntaxhighlight lang="c++">
  <syntaxhighlight lang="c++">
68行目: 95行目:
  #include <fstream>
  #include <fstream>
  #include "nlohmann/json.hpp"
  #include "nlohmann/json.hpp"
using namaspace std;
   
   
  // JSONファイルの読み込み
  // JSONファイルの読み込み
101行目: 130行目:
  #include <fstream>
  #include <fstream>
  #include "nlohmann/json.hpp"
  #include "nlohmann/json.hpp"
using namaspace std;
   
   
  // 書き込むJSONファイルの内容
  // 書き込むJSONファイルの内容
123行目: 154行目:
==== JSONファイルの編集 ====
==== JSONファイルの編集 ====
以下の例では、"Age"キーの値を30に変更、"Height"キーと値を削除している。<br>
以下の例では、"Age"キーの値を30に変更、"Height"キーと値を削除している。<br>
<br>
<syntaxhighlight lang="json">
# 編集前のJSONデータ (JSON-Sample.jsonファイル)
{
    "FirstName": "Jung kook",
    "LastName":  "Park",
    "Country":  "Korea",
    "Age":      25,
    "Height":    180.5
}
</syntaxhighlight>
<br>
<br>
  <syntaxhighlight lang="c++">
  <syntaxhighlight lang="c++">
128行目: 171行目:
  #include <fstream>
  #include <fstream>
  #include "nlohmann/json.hpp"
  #include "nlohmann/json.hpp"
using namaspace std;
   
   
  using json = nlohmann::json;
  using json = nlohmann::json;
using namespace std;
   
   
  int main()
  // JSONファイルの読み込み
  {
string filename = "JSON-Sample.json";
     // 元のJSONデータ
ifstream ifs(filename.c_str());
     json m_json = {
      {"FirstName", "Jung kook"},
  if (ifs.good()) {
      {"LastName", "Park"},
     json m_json;
      {"Country",  "Korea"},
     ifs >> m_json;
      {"Age",      25},
   
      {"Height",    180.5f}
    // ファイルを閉じる
    };
    ifs.close();
   
   
     // Ageを30に変更
     // Ageを30に変更
150行目: 194行目:
   
   
     // 変更したJSONをファイルに書き込み
     // 変更したJSONをファイルに書き込み
    string filename = "JSON-Sample.json";
     ofstream writing_file;
     ofstream writing_file;
     writing_file.open(filename, ios::out);
     writing_file.open(filename, ios::out);
     writing_file << m_json.dump(4) << endl; // dump(4)でインデント付きで整形出力
     writing_file << m_json.dump() << endl;
     writing_file.close();
     writing_file.close();
   
  }
     return 0;
else {
    // JSONファイルの読み込みに失敗した場合
     cout << "ファイルの読み込みに失敗しました" << endl;
  }
  }
  </syntaxhighlight>
  </syntaxhighlight>