QJsonDocument Class

QJsonDocument クラスは、JSON ドキュメントを読み書きする方法を提供します。詳細...

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

このクラスは等価比較可能です。

注意:このクラスの関数はすべてリエントラントです。

パブリック型

enum JsonFormat { Indented, Compact }

パブリック関数

QJsonDocument()
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 = 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] 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)

Move-construct QJsonDocument fromother.

[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()、QJsonParseErrorisNull()も参照

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

QVariant variant からQJsonDocument を作成する。

variantQVariantMapQVariantHashQVariantListQStringList 以外の型が含まれている場合、返されるドキュメントは無効です。

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 = Indented) const

指定されたformatQJsonDocument を UTF-8 でエンコードされた JSON ドキュメントに変換します。

fromJson() およびJsonFormatも参照

QVariant QJsonDocument::toVariant() const

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

返される variant は、ドキュメントが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() が偽の場合、返されるQJsonValueQJsonValue::Undefined となります。

QJsonValueQJsonValue::isUndefined()、QJsonObjectも参照

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

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

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

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

QJsonValueQJsonValue::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 を返す。

© 2025 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.