【发布时间】:2021-07-18 20:18:35
【问题描述】:
我希望你在每件事上都做得很好,实际上我想以这些 Jason 格式访问 countryId(嵌套字典)中的国家名称,我测试了不同的方式,但不幸的是我不明白解决方案,你能帮忙吗我来解决吗? 这是我的 json :
[
{
"_id": "606836792ed79e4194e98848",
"title": "berlin",
"countryId": {
"_id": "6068366e2ed79e4194e98847",
"name": "germany",
"__v": 0
},
"__v": 0
},
{
"_id": "60505f8c8acc922dc46a60eb",
"title": "ezmir",
"countryId": {
"_id": "60505f808acc922dc46a60ea",
"name": "turkey",
"__v": 0
},
"__v": 0
},
{
"_id": "606836942ed79e4194e9884b",
"title": "karaj",
"countryId": {
"_id": "606836832ed79e4194e98849",
"name": "iran",
"__v": 0
},
"__v": 0
},
{
"_id": "60696c7b1c7a4937a817c463",
"title": "new york",
"countryId": {
"_id": "60696c461c7a4937a817c462",
"name": "usa",
"__v": 0
},
"__v": 0
},
{
"_id": "60696c0d1c7a4937a817c461",
"title": "stanbol",
"countryId": {
"_id": "60505f808acc922dc46a60ea",
"name": "turkey",
"__v": 0
},
"__v": 0
},
{
"_id": "6068368d2ed79e4194e9884a",
"title": "tabriz",
"countryId": {
"_id": "606836832ed79e4194e98849",
"name": "iran",
"__v": 0
},
"__v": 0
}
]
这也是我想在我的代码中解决它的地方:
func fetchingcityInformation() {
let url = "http://192.168.1.199:2581/api/citys"
do {
let data = try Data(contentsOf: URL(string: url)!)
let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers)
guard let array = json as? [Any] else { return }
print("the json file is : \(array)")
print(" country is folan : \(array[2]) ")
for users in array {
guard let usersDict = users as? [String: Any] else { return }
guard let names = usersDict["countryId"] as? [String : String] else { return }
print(" userDict is : \(String(describing: usersDict["countryId"])) ")
print("the country id is : \(names)")
// inja boodi
//guard var countryId = user as? [String: Any] else { return }
//countryArray = names
if countryPressed == countryArray["name"] {
guard (usersDict["title"] as? String) != nil else {
return
}
cityslbl.append(title!)
}
}
【问题讨论】:
-
请编辑您的帖子以包含代码而不是代码图片,这不利于搜索、屏幕阅读器、复制和粘贴等。
标签: json swift dictionary