2016-04-09 22 views
1

ile this göre, bir JSON braket geçerlidir başlayarak, bu yüzden bir test.json dosyada, öğenin bir listesini kodlanmış adres:Ayrıştırma JSON parantez ile başlayan `[` Qt5

[{"name": "a"},{"name": "b"}] 

Ağır bundan inspire answer i main.cpp bu kodu itmek:

File content: "[{\"name\": \"a\"},{\"name\": \"b\"}]\n" 
sett2: QJsonObject() (empty: true) 
name value: QJsonValue(undefined) 
QJsonObject of accessed value: QJsonObject() 
: Burada
#include <QApplication> 
#include <QFile> 
#include <QByteArray> 
#include <QJsonObject> 
#include <QJsonDocument> 
#include <QVariant> 
#include <QDebug> 
#include <iostream> 


int main(int argc, char *argv[]) { 
     // Reading the JSON, parse it, get data as QJsonObject 
     QString val; 
     QFile file; 
     file.setFileName("test.json"); 
     file.open(QIODevice::ReadOnly | QIODevice::Text); 
     val = file.readAll(); 
     file.close(); 
     QJsonDocument d = QJsonDocument::fromJson(val.toUtf8()); 
     QJsonObject sett2 = d.object(); 

     // Printings 
     qWarning() << "File content: " << val; 
     qWarning() << "sett2: " << sett2 << " (empty: " << sett2.empty() << ')'; 

     // try to access the data directly 
     QJsonValue value = sett2.value(QString("name")); 
     qWarning() << "name value: " << value; 
     QJsonObject item = value.toObject(); 
     qWarning() << "QJsonObject of accessed value: " << item; 
} 

çıkışı

Dosyanın düzgün okunduğunu görüyoruz. Ancak, hiçbir veriye erişilemiyor: sett2 boş, sanki hiç veri yokmuş gibi. Tarlalara bir erişim value(), yöntem izin görünüyor tek:

QJsonObject belgelerinde arama yaptıktan sonra ben bu durumda, dosyadaki verilere erişim verebilir herhangi rutin bulundu olamaz ama bir parametreye ihtiyacı var. 0, 1, NULL"name", "a", "b" ve "knock knock" numaralı telefonlarda derleme hatası veya boş verilere yol açabilir. keys() gibi diğer yöntemler de boş veriyi döndürür.

Nesnelerin erişim verileri nasıl? (Burada, name: "a" ve name: "b")

cevap