c++jsonblackberryblackberry-10blackberry-cascades

Read data from JSON file?


QVariant list = ja.loadFromBuffer(buffer);
QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);                      
AbstractPane *root = qml->createRootObject<AbstractPane>();
ListView *listView = root->findChild<ListView*>("liste");
GroupDataModel *model = new GroupDataModel(QStringList() << "nom" );
QVariantMap addresses = list.toMap();

foreach(QVariant var, addresses) {
    QVariantMap addressMap = var.toMap();
    qDebug() << "CategoryName is " <<addresses;
    model->insert(addressMap);
}

listView->setDataModel(model);

When I run this the result that appear :

 QMap(("test", QVariant(QVariantList, (QVariant(QVariantMap, QMap(("id", QVariant(QString, "1") ) ( "nom" ,  QVariant(QString, "samar") ) ( "prenom" ,  QVariant(QString, "20") ) )  ) ,  QVariant(QVariantMap, QMap(("id", QVariant(QString, "11") ) ( "nom" ,  QVariant(QString, "sarra") ) ( "prenom" ,  QVariant(QString, "1") ) )  ) ,  QVariant(QVariantMap, QMap(("id", QVariant(QString, "21") ) ( "nom" ,  QVariant(QString, "akgc") ) ( "prenom" ,  QVariant(QString, "3") ) )  ) ,  QVariant(QVar...
  1. My question is how can i get the "nom" and "prenom" in my Json File

  2. My JSON file URL: http://trackanddragutils.azurewebsites.net/json/json.php

Thanks for any help!! :i)


Solution

  • Try this:

    QVariantList testList = address["test"].toList();
    QVariantMap map = testList[0].toMap();
    
    qDebug() << "Nom is " << map["nom"].toString();
    qDebug() << "Prenom is " << map["prenom"].toString();