如何从Qt c ++中的json文件中获取所有对象名称

时间:2015-04-13 07:43:45

标签: json qt

以下是我的json文件,我想要字符串" ID_OK"和" ID_CANCEL"我将用它来与其他一些json文件进行比较。

所以如何才能获得这些字符串.plz帮帮我。谢谢

{
 "Dictionary": {
    "EnglishName": "English (United States",
    "CultureName": "English (United States)",
    "Culture": "en-US",
    "ID_OK": {
       "Translation": "OK",
      "English": "OK"
    },
    "ID_CANCEL": {
      "Translation": "Cancel", 
      "English": "Cancel" 
    }


  }
}

我写的是,

    QJsonObject jsonObject = sd.object();
    QJsonObject sett3 = jsonObject["Dictionary"].toObject();

qDebug()<<sett3.value("Dictionary").toString();

它为我提供了所有带有键,值对的对象名称,

1 个答案:

答案 0 :(得分:0)

您可以使用SubObjects。请尝试以下示例:

QString Str = QString("{"
        "\"Dictionary\": {"
        "\"EnglishName\": \"English (United States\","
        "   \"CultureName\" : \"English (United States)\","
        "   \"Culture\" : \"en-US\","
        "   \"ID_OK\" : {"
        "   \"Translation\": \"OK\","
        "       \"English\" : \"OK\"""},"
        "\"ID_CANCEL\" : {"
        "       \"Translation\": \"Cancel\","
        "           \"English\" : \"Cancel\""
        "   }"
        "}"
        "}");

    QJsonParseError error;
    QJsonDocument Doc = QJsonDocument::fromJson(Str.toUtf8(), &error);
    if (error.error != QJsonParseError::NoError)
        return;
    QJsonObject jsonObject = Doc.object();
    QJsonObject DictonarySubObject= jsonObject.value("Dictionary").toObject();

    QJsonObject CancelSubObject = DictonarySubObject.value("ID_CANCEL").toObject();
    qDebug() << CancelSubObject.value("Translation").toString();
    qDebug() << CancelSubObject.value("English").toString();

    QJsonObject OkSubObject = DictonarySubObject.value("ID_OK").toObject();
    qDebug() << OkSubObject.value("Translation").toString();
    qDebug() << OkSubObject.value("English").toString();