0
我試圖從幾天後的下面的json文件中獲取「名稱」,但沒有成功。任何人都可以請指導我在正確的方向嗎?我設法訪問結果數組,但不是「名稱」變量。用swift解析json 3對我不起作用
{
"count": 7,
"next": null,
"previous": null,
"results": [
{
"id": 10,
"name": "Abs"
},
{
"id": 8,
"name": "Arms"
},
{
"id": 12,
"name": "Back"
},
{
"id": 14,
"name": "Calves"
},
{
"id": 11,
"name": "Chest"
},
{
"id": 9,
"name": "Legs"
},
{
"id": 13,
"name": "Shoulders"
}
]
}
和我的代碼看起來像這樣
let url = URL(string: "https://wger.de/api/v2/exercisecategory")!
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
if error != nil {
print(error!)
} else {
if let content = data {
do {
let json = try? JSONSerialization.jsonObject(with: content, options:[]) {
if let content = json as? [String: Any] {
if let results = content["results"] as? [Any] {
for result in results {
if let bodypart = result["name"] as! [String: Any] {
print(bodypart)
}
}
}
}
}
}
}
}
}
非常感謝您的意見,它的工作原理與我現在想要的一樣! – Niklas
@Niklas歡迎伴侶:) –