【发布时间】:2016-05-25 13:52:06
【问题描述】:
我的代码中有一个 QJsonObject,它看起来类似于下面发布的内容(一旦在 python 中转换为字符串),(具有更多相同形式的条目)。假设我想将所有名称属性分组到一个数组中。在 python 中,我会将我的 Json 字符串转换为字典,然后按如下方式填充此列表。如何使用 qt 在 c++ 中做到这一点?
1.
CountryNameList = []
For i in range len(CountryDictionary[1]):
CountryNameList.append CountryDictionary[1][i]["name"]
// 下面是 JSON 文件的样子:
[
{
"per_page": "50",
"total": 264,
"page": 1,
"pages": 6
},
[
{
"longitude": "-70.0167",
"name": "Aruba",
"region": {
"id": "LCN",
"value": "Latin America & Caribbean (all income levels)"
},
"adminregion": {
"id": "",
"value": ""
},
"iso2Code": "AW",
"capitalCity": "Oranjestad",
"latitude": "12.5167",
"incomeLevel": {
"id": "NOC",
"value": "High income: nonOECD"
},
"id": "ABW",
"lendingType": {
"id": "LNX",
"value": "Not classified"
}
},
{
"longitude": "69.1761",
"name": "Afghanistan",
"region": {
"id": "SAS",
"value": "South Asia"
},
"adminregion": {
"id": "SAS",
"value": "South Asia"
},
"iso2Code": "AF",
"capitalCity": "Kabul",
"latitude": "34.5228",
"incomeLevel": {
"id": "LIC",
"value": "Low income"
},
"id": "AFG",
"lendingType": {
"id": "IDX",
"value": "IDA"
}
},
] ]
【问题讨论】:
-
用
QJsonDocument::fromJson()创建一个QJsonDocument -
@KarstenKoop 转换后如何以上述方式访问元素。
-
和一些
ToQVariantMap? -
QJsonDocument::array() 为您提供一个可以迭代的数组,例如使用
foreach -
@KarstenKoop 我成功创建了一个数组,然后访问了第一个元素,但是如何将第一个元素(即 JsonValue)转换为字典或访问其每个“名称”属性,具体取决于在索引上。