1

我有兴趣了解我们如何使用 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();
4

3 回答 3

8

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

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"
于 2016-09-16T05:43:15.033 回答
2

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

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

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

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

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

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

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

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

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

于 2016-09-16T08:56:09.017 回答
0

您的 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());
        }

    }

};
于 2019-04-08T08:43:23.033 回答