我尝试在 json 中读取 URL 请求(在字典“车辆”中),但数组将为空 这是 json,我需要阅读车辆中的字典和 url
{
"count": 87,
"next": "https://swapi.co/api/people/?page=2",
"previous": null,
"results": [
{
"name": "Luke Skywalker",
"height": "172",
"mass": "77",
"hair_color": "blond",
"skin_color": "fair",
"eye_color": "blue",
"birth_year": "19BBY",
"gender": "male",
"homeworld": "https://swapi.co/api/planets/1/",
"films": [
"https://swapi.co/api/films/2/",
"https://swapi.co/api/films/6/",
"https://swapi.co/api/films/3/",
"https://swapi.co/api/films/1/",
"https://swapi.co/api/films/7/"
],
"species": [
"https://swapi.co/api/species/1/"
],
"vehicles": [
"https://swapi.co/api/vehicles/14/",
"https://swapi.co/api/vehicles/30/"
],
这是我尝试的代码
if let results = json["results"] as? [[String : AnyObject]] {
var finalArray2 : [String] = []
for result in results {
if let dict = result as? [String: Any], let vehicles = dict["vehicles"] as? String{
self.URLveicoli = vehicles
//print(self.nomepersonaggio)
print(self.URLveicoli)
//finalArray.append(name)
finalArray2.append(vehicles)
}
}
print(finalArray2)
}
最佳答案
在 json 中,vehicles
是一个字符串数组(url)而不是一个字符串。
改变这个
let vehicles = dict["vehicles"] as? String
到
let vehicles = dict["vehicles"] as? [String]
并追加车辆数组
finalArray2.append(contentsOf: vehicles)
关于json - 快速读取 JSON 文件中的 URL HTTPS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47648898/