このページでは

QJsonDocument Class

QJsonDocument クラスは、JSON ドキュメントの読み取りおよび書き込みを行う手段を提供します。詳細...

ヘッダー: #include <QJsonDocument>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core

注:このクラスのすべての関数は再入可能です。

QJsonDocument の比較

カテゴリ比較可能な型
等価性QJsonDocument

パブリック型

enum JsonFormat { Indented, Compact }

パブリック関数

QJsonDocument()
(since 6.12) QJsonDocument(QJsonArray &&array)
(since 6.12) QJsonDocument(QJsonObject &&object)
QJsonDocument(const QJsonArray &array)
QJsonDocument(const QJsonObject &object)
QJsonDocument(const QJsonDocument &other)
QJsonDocument(QJsonDocument &&other)
~QJsonDocument()
QJsonArray array() const
bool isArray() const
bool isEmpty() const
bool isNull() const
bool isObject() const
QJsonObject object() const
void setArray(const QJsonArray &array)
void setObject(const QJsonObject &object)
void swap(QJsonDocument &other)
QByteArray toJson(QJsonDocument::JsonFormat format = JsonFormat::Indented) const
QVariant toVariant() const
QJsonDocument &operator=(QJsonDocument &&other)
QJsonDocument &operator=(const QJsonDocument &other)
const QJsonValue operator[](const QString &key) const
const QJsonValue operator[](qsizetype i) const
const QJsonValue operator[](QLatin1StringView key) const
const QJsonValue operator[](QStringView key) const

静的パブリックメンバー

QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error = nullptr)
QJsonDocument fromVariant(const QVariant &variant)
bool operator!=(const QJsonDocument &lhs, const QJsonDocument &rhs)
bool operator==(const QJsonDocument &lhs, const QJsonDocument &rhs)

詳細な説明

QJsonDocument は、完全な JSON ドキュメントをラップするクラスであり、UTF-8 エンコードされたテキスト形式の表現からこのドキュメントを読み込んだり、そこに書き込んだりすることができます。

JSON ドキュメントは、QJsonDocument::fromJson() を使用して、テキストベースの表現から QJsonDocument へ変換することができます。toJson() は、それをテキストに戻します。このパーサーは非常に高速かつ効率的で、JSON を Qt で使用されるバイナリ表現に変換します。

解析されたドキュメントの有効性は、!isNull() を使用して確認できます。

ドキュメントに配列が含まれているか、オブジェクトが含まれているかは、isArray() およびisObject() を使用して確認できます。ドキュメントに含まれる配列やオブジェクトは、array() またはobject() を使用して取得し、読み取りや操作を行うことができます。

「Qt における JSON サポート」および「ゲームの保存と読み込み」も参照してください 。

メンバ型のドキュメント

enum QJsonDocument::JsonFormat

この値は、toJson() を使用してQJsonDocument に変換する際に生成されるJSONバイト配列の形式を定義します。

定数定数定数値
QJsonDocument::Indented0次のように、人間が読みやすい出力を定義します:
    {
        "Array": [
            true,
            999,
            "string"
        ],
        "Key": "Value",
        "null": null
    }
QJsonDocument::Compact1以下のように、簡潔な出力を定義します:
    {"Array":[true,999,"string"],"Key":"Value","null":null}

メンバ関数のドキュメント

QJsonDocument::QJsonDocument()

空で無効なドキュメントを作成します。

[explicit, since 6.12] QJsonDocument::QJsonDocument(QJsonArray &&array)

array から QJsonDocument を生成します。

この関数は Qt 6.12 で導入されました。

[explicit, since 6.12] QJsonDocument::QJsonDocument(QJsonObject &&object)

object から QJsonDocument を作成します。

この関数は Qt 6.12 で導入されました。

[explicit] QJsonDocument::QJsonDocument(const QJsonArray &array)

array から QJsonDocument を生成します。

[explicit] QJsonDocument::QJsonDocument(const QJsonObject &object)

object から QJsonDocument を作成します。

QJsonDocument::QJsonDocument(const QJsonDocument &other)

other ドキュメントのコピーを作成します。

[noexcept] QJsonDocument::QJsonDocument(QJsonDocument &&other)

other から QJsonDocument をムーブコンストラクトで生成します。

[noexcept] QJsonDocument::~QJsonDocument()

ドキュメントを削除します。

fromRawData で作成されたバイナリデータセットは解放されません。

QJsonArray QJsonDocument::array() const

ドキュメントに含まれるQJsonArray を返します。

ドキュメントにオブジェクトが含まれている場合は、空の配列を返します。

isArray()、object()、およびsetArray()も参照してください 。

[static] QJsonDocument QJsonDocument::fromJson(const QByteArray &json, QJsonParseError *error = nullptr)

json を UTF-8 エンコードされた JSON ドキュメントとして解析し、そこからQJsonDocument を作成します。

解析に成功した場合、有効な(null ではない)QJsonDocument を返します。失敗した場合は、返されるドキュメントは null となり、オプションのerror 変数にはエラーに関する詳細情報が格納されます。

toJson()、QJsonParseError 、およびisNull()も参照してください 。

[static] QJsonDocument QJsonDocument::fromVariant(const QVariant &variant)

QVariant `variant` から `QJsonDocument ` を作成します。

variant に、QVariantMap 、QVariantHash 、QVariantList 、またはQStringList 以外の型が含まれている場合、返されるドキュメントは無効となります。

toVariant()も参照してください 。

bool QJsonDocument::isArray() const

ドキュメントに配列が含まれている場合、true を返します。

array() およびisObject()も参照してください 。

bool QJsonDocument::isEmpty() const

ドキュメントにデータが含まれていない場合、true を返します。

bool QJsonDocument::isNull() const

このドキュメントがnullの場合、true を返します。

null ドキュメントとは、デフォルトコンストラクタによって作成されたドキュメントのことです。

UTF-8 エンコードされたテキストまたはバイナリ形式から作成されたドキュメントは、解析中に検証が行われます。検証に失敗した場合、返されるドキュメントも null になります。

bool QJsonDocument::isObject() const

ドキュメントにオブジェクトが含まれている場合、true を返します。

object() およびisArray()も参照してください 。

QJsonObject QJsonDocument::object() const

ドキュメントに含まれるQJsonObject を返します。

ドキュメントに配列が含まれている場合は、空のオブジェクトを返します。

isObject()、array()、およびsetObject()も参照してください 。

void QJsonDocument::setArray(const QJsonArray &array)

array をこのドキュメントのメインオブジェクトとして設定します。

setObject() およびarray()も参照してください 。

void QJsonDocument::setObject(const QJsonObject &object)

object をこのドキュメントのメインオブジェクトとして設定します。

setArray() およびobject()も参照してください 。

[noexcept] void QJsonDocument::swap(QJsonDocument &other)

このドキュメントをother と置き換えます。この操作は非常に高速で、失敗することはありません。

QByteArray QJsonDocument::toJson(QJsonDocument::JsonFormat format = JsonFormat::Indented) const

QJsonDocument を、指定されたformat 内のUTF-8エンコードされたJSONドキュメントに変換します。

fromJson() およびJsonFormatも参照してください 。

QVariant QJsonDocument::toVariant() const

JSONドキュメントを表すQVariant を返します。

ドキュメントが `QJsonArray ` の場合は `QVariantList ` が、`QJsonObject` の場合は `QVariantMap ` が返されます。

fromVariant() およびQJsonValue::toVariant()も参照してください 。

[noexcept] QJsonDocument &QJsonDocument::operator=(QJsonDocument &&other)

other をこのドキュメントに割り当てます。

QJsonDocument &QJsonDocument::operator=(const QJsonDocument &other)

other ドキュメントをこのQJsonDocument に割り当てます。このオブジェクトへの参照を返します。

const QJsonValue QJsonDocument::operator[](const QString &key) const

キー `key` の値を表す `QJsonValue ` を返します。

object().value(key) を呼び出すのと同じです。

キーが存在しない場合、または `isObject()` が `false` の場合、返される `QJsonValue ` は `QJsonValue::Undefined ` となります。

QJsonValue 、QJsonValue::isUndefined()、およびQJsonObjectも参照してください 。

const QJsonValue QJsonDocument::operator[](qsizetype i) const

インデックスi の値を表すQJsonValue を返します。

array().at(i) を呼び出すのと同じです。

i が範囲外の場合、またはisArray()がfalseの場合、返されるQJsonValue はQJsonValue::Undefined となります。

QJsonValue 、QJsonValue::isUndefined()、およびQJsonArrayも参照してください 。

const QJsonValue QJsonDocument::operator[](QLatin1StringView key) const

これはオーバーロードされた関数です。

const QJsonValue QJsonDocument::operator[](QStringView key) const

これはオーバーロードされた関数です。

関連する非メンバー

[noexcept] bool operator!=(const QJsonDocument &lhs, const QJsonDocument &rhs)

lhs のドキュメントがrhs のドキュメントと一致しない場合はtrue を返し、それ以外の場合はfalse を返します。

[noexcept] bool operator==(const QJsonDocument &lhs, const QJsonDocument &rhs)

lhs のドキュメントがrhs のドキュメントと等しい場合は `true ` を返し、それ以外の場合は `false ` を返します。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.