【问题标题】:Parse nested JSON with QJsonDocument in Qt在 Qt 中使用 QJsonDocument 解析嵌套的 JSON
【发布时间】:2016-09-16 05:19:30
【问题描述】:

我有兴趣了解我们如何使用 Qt 的 QJsonDocument解析来自简单 嵌套 JSON 的所有条目(因为我刚刚开始研究这个)。

嵌套 json 示例

{
    "city": "London",
    "time": "16:42",
    "unit_data": 
        [
            {
                "unit_data_id": "ABC123",
                "unit_data_number": "21"
            },
            {
                "unit_data_id": "DEF456",
                "unit_data_number": "12"
            }
        ]
}

我可以像这样解析它的非嵌套部分:

QJsonObject jObj;
QString city = jObj["city"].toString();
QString time = jObj["time"].toString();

【问题讨论】:

    标签: c++ json qt qt5.7


    【解决方案1】:

    我不确定你在问什么,但也许这可能会有所帮助:

    QJsonDocument doc;
    doc = QJsonDocument::fromJson("{                                              "
                                  "     \"city\": \"London\",                      "
                                  "     \"time\": \"16:42\",                       "
                                  "     \"unit_data\":                             "
                                  "         [                                      "
                                  "             {                                  "
                                  "                 \"unit_data_id\": \"ABC123\",  "
                                  "                 \"unit_data_number\": \"21\"   "
                                  "             },                                 "
                                  "             {                                  "
                                  "                 \"unit_data_id\": \"DEF456\",  "
                                  "                 \"unit_data_number\": \"12\"   "
                                  "             }                                  "
                                  "         ]                                      "
                                  " }");
    
    // This part you have covered
    QJsonObject jObj = doc.object();
    qDebug() << "city" << jObj["city"].toString();
    qDebug() << "time" << jObj["time"].toString();
    // Since unit_data is an array, you need to get it as such
    QJsonArray array = jObj["unit_data"].toArray();
    // Then you can manually access the elements in the array
    QJsonObject ar1 = array.at(0).toObject();
    qDebug() << "" << ar1["unit_data_id"].toString();
    // Or you can loop over the items in the array
    int idx = 0;
    for(const QJsonValue& val: array) {
        QJsonObject loopObj = val.toObject();
        qDebug() << "[" << idx << "] unit_data_id    : " << loopObj["unit_data_id"].toString();
        qDebug() << "[" << idx << "] unit_data_number: " << loopObj["unit_data_number"].toString();
        ++idx;
    }
    

    我得到的输出是:

    city "London"
    time "16:42"
    "ABC123"
    [ 0 ] unit_data_id    :  "ABC123"
    [ 0 ] unit_data_number:  "21"
    [ 1 ] unit_data_id    :  "DEF456"
    [ 1 ] unit_data_number:  "12"
    

    【讨论】:

    • 快速跟进问题:我们如何检查/确保QJsonArray array.at(i) 不会超出范围,即上面示例中的i&gt;=2
    • @nk-fford 您可以使用QJsonArray::count() 函数获取 QJsonArray 的大小。答案中显示的基于范围不应超出范围。
    【解决方案2】:

    在 JSON 表示法中,所有内容都应以键值对格式进行格式化。 Keys 总是字符串,但 values 可以是字符串文字 ("example")、数字文字、数组 ([]) 和对象 ({})。 p>

    QJsonDocument::fromJson(...).object() 返回给定 JSON 字符串的 根对象。回想一下,对象是用{} 表示法编写的。此方法为您提供QJsonObject。这个 JSON object 有 3 个 keys"city""name""unit_data")其中的 value 是字符串字面量类型, 字符串字面量和数组。

    因此,如果您想访问存储在该数组中的数据,您应该这样做:

    QJsonArray array = rootObj["unit_data"].toArray();
    

    请注意,数组没有,它们只有,可能属于上述三种类型。在这种情况下,该数组包含 2 个 objects,它们可以被视为其他 JSON 对象。所以,

    QJsonObject obj = array.at(0).toObject();
    

    现在obj 对象指向以下对象:

    {
        "unit_data_id": "ABC123",
        "unit_data_number": "21"
    }
    

    所以,你现在应该可以做你想做的事了。 :)

    【讨论】:

      【解决方案3】:

      您的 JSON 中的一个元素可能包含更多元素。也可能发生你不知道文件的特征(或者你想要一个通用的功能)。

      因此,您可以对任何 JSON 使用函数:

      void traversJson(QJsonObject json_obj){
          foreach(const QString& key, json_obj.keys()) {
      
              QJsonValue value = json_obj.value(key);
              if(!value.isObject() ){
                qDebug() << "Key = " << key << ", Value = " << value;
               }
              else{
                   qDebug() << "Nested Key = " << key;
                   traversJson(value.toObject());
              }
      
          }
      
      };
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-09-12
        • 1970-01-01
        • 1970-01-01
        • 2019-08-20
        • 2015-08-28
        • 2017-08-03
        • 2018-10-06
        相关资源
        最近更新 更多