:
になり
#include <QCoreApplication>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonValue>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
char json[] = "{ id: 12}";
QJsonDocument doc = QJsonDocument::fromJson(json);
QJsonValue v_ar = doc.object()["no-defined"];
QJsonValue v_o = doc.object().value(QString("no-defined"));
qDebug() << "Array-operator:" << v_ar.isUndefined() << v_ar.toString() << v_ar.type(); //returns: false ""
qDebug() << "Object-operator:" << v_o.isUndefined() << v_o.toString(); //returns true ""
qDebug() << "direct object call:" << doc.object().value("no-defined").isUndefined() << doc.object().value("no-defined").toString(); //returns true ""
qDebug() << "direct array call:" << doc.object()["no-defined"].isUndefined() << doc.object()["no-defined"].toString(); //returns false ""
return a.exec();
}
:
Array-operator: false "" 0
Object-operator: true ""
direct object call: true ""
direct array call: false ""
あなたは配列演算子に何か問題があることがわかります。 JsonValue :: Undefinedを返す代わりに、JsonValue :: NULL(0)を返します。周り
QJsonValue v = doc.object().value("no-defined");
以上を使用するような方法:
Q_ASSERT(doc.object().value("no-defined").isUndefined());
QT-BUGによると、それはそのように意図されます。配列演算子を呼び出すと、デフォルト値のNULLを持つ新しい項目が作成されます。