2016-08-04 5 views
0

QTで以下に示すようなjsonファイルを読み込もうとしています。誰かがjsonオブジェクトから値を取得し、test_cell2.CELLS [0]や何らかの方法で別のコンテナや配列に格納する方法を提案することができます。これにより、ネストも処理できるようになりますし、ファイルの解析後に簡単にアクセスできますQT JSONファイルを読み込んで値を保存して検索する

"test_cells2" : { 
     "CELLS" : { 
      "cell_0" : { 
       "prettyName" : "cell_1", 
       "CELLS" : { 
        "cell_1" : { 
         "prettyName" : "cell_1", 
         "type" : "default", 
        }, 
       }, 
      }, 
      "cell_1" : { 
       "prettyName" : "cell_1", 
       "type" : "default", 
      }, 
      "cell_2" : { 
       "type" : "text cell ko", 
      }, 
      "cell_3" : { 
       "prettyName" : "cell_3", 
       "type" : "default", 
      }, 
      "cell_4" : { 
       "data" : { 
        "settings" : { 
         "EXEC_PARAMETERS" : { 
          "defaultQueue" : "batch", 
          "environment" : { 
           "blabla" : "blabla2", 
          }, 
         }, 
        }, 
       }, 
       "type" : "parallel_test", 
      }, 
     }, 
    }, 
+0

可能な重複http://stackoverflow.com/questions/15893040を/ how-to-create-read-write-json-files-in-qt5) –

答えて

1

Qtバージョン> 5.5の場合、QJSonDocument、http://doc.qt.io/qt-5/qjsondocument.htmlを確認してください。

アン例:

// Just read it from a file... or copy here   
    const QString yourJSON = "..."; 
    const QJsonDocument jsonDoc = QJsonDocument::fromJson(yourJSON.toLocal8Bit()); 
    const QVariantMap map = jsonDoc.toVariant().toMap(); 

最初にあなたの細胞の配列をロードし、細胞内CELLSのネストの世話をするために:

// Start read your parameter 
    const QVariant testCells2 = map["test_cells2"].toVariant(); 
    const QVariantList CELLS = testCells2.toList(); 
    // Now, we have a list available CELLS, check one be one 
    foreach (const QVariant& cell, CELLS) { 
     const QVariantMap wrapped = cell.toMap(); 
     qDebug() << "Pretty Name: " << wrapped["prettyName"].toString(); 
     qDebug() << "Type: " << wrapped["type"].toString(); 
     const hasList = !wrapped["CELLS"].toList().isEmpty(); 
     qDebug() << "Has child list? << hasList; 
     if (hasList) { 
      // Start another loop to check the child list. 
     } 
    } 
+0

CELLS内のCELLSのネスティングの処理方法 – leo

+0

QVariant、QVariantList、QVariantMapで動作するようにいくつかの変更を加えました。 – mohabouje

3

は、この機能

QJsonDocumentを見てみましょう:: fromJson(QByteArray)

http://doc.qt.io/qt-5/qjsondocument.html#fromJson

その後QJsonDocument::object()を使用すると、あなたの値を取得するためにキーを使用することができます。

QJsonDocument doc = QJsonDocument::fromJson(QByteArray); 
QJsonObject root = doc.object(); 
foreach(QJsonValue element, root["CELLS"].toArray()){ 
    QJsonObject node = element.toObject(); 
    node["whatEver"]; 

} 
[Qt5で作成する方法/リード/ライトJSONファイル](の
関連する問題