2016-04-12 1 views
0
#include <QCoreApplication> 
#include <QJsonDocument> 
#include <QJsonValue> 
#include <QJsonObject> 

int main(int argc, char *argv[]) 
{ 
    QCoreApplication a(argc, argv); 
    char json[] = "{ id: 12}"; 
    QJsonDocument doc = QJsonDocument::fromJson(json); 
    QJsonValue v = doc.object()["no-defined"]; 
    Q_ASSERT(v.isUndefined()); // assert failed. 

    return a.exec(); 
} 

私はキーを定義しますが、テストコードでは、真、偽のない返しv.isUndefined()かどうかを判断するためにQJsonValue::isUndefined()を使用したい動作しません。私は理由を知らない、何か説明がありますか?テストを実行した後QJsonValue :: isUndefinedは正しく

答えて

1

になり
#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を持つ新しい項目が作成されます。