动态地将 JSON 中的结果解析为列表
Parse results from JSON to a list dynamically
我做了一个 JSON 请求并获得了一些信息。
func parseJSON(poiData: Data) {
let decoder = JSONDecoder()
do {
let decodedData = try decoder.decode(POIData.self, from: poiData)
POIManager.POIname_One = decodedData.results[0].name
POIManager.POIplaceid_One = decodedData.results[4].place_id
POIManager.POIvicinity_One = decodedData.results[4].vicinity
POIManager.POIlong_One = decodedData.results[0].geometry.location.lat
POIManager.POIlat_One = decodedData.results[0].geometry.location.lng
POIManager.POIname_Two = decodedData.results[1].name
POIManager.POIplaceid_Two = decodedData.results[1].place_id
POIManager.POIvicinity_Two = decodedData.results[1].vicinity
POIManager.POIlong_Two = decodedData.results[1].geometry.location.lat
POIManager.POIlat_Two = decodedData.results[1].geometry.location.lng
POIManager.POIname_Three = decodedData.results[2].name
POIManager.POIplaceid_Three = decodedData.results[2].place_id
POIManager.POIvicinity_Three = decodedData.results[2].vicinity
POIManager.POIlong_Three = decodedData.results[2].geometry.location.lat
POIManager.POIlat_Three = decodedData.results[2].geometry.location.lng
} catch {
print(error)
}
}
在另一个 Swift 文件中,它将请求的结果放在如下列表中:
@IBAction func kategorieEins(_ sender: UIButton) {
//Eigene Standort soll hier gezeigt werden/aktualisierter Standort
locationManager.delegate=self
let marker1 = GMSMarker()
marker1.position = CLLocationCoordinate2D(latitude: POIManager.POIlong_One, longitude: POIManager.POIlat_One)
marker1.title = POIManager.POIname_One
marker1.snippet = "Marker1_0"
marker1.map = mapView
let marker2 = GMSMarker()
marker2.position = CLLocationCoordinate2D(latitude: POIManager.POIlong_Two, longitude: POIManager.POIlat_Two)
marker2.title = POIManager.POIname_Two
marker2.snippet = "Marker2_0"
marker2.map = mapView
let marker3 = GMSMarker()
marker3.position = CLLocationCoordinate2D(latitude: POIManager.POIlong_Three, longitude: POIManager.POIlat_Three)
marker3.title = POIManager.POIname_Three
marker3.snippet = "Marker3_0"
marker3.map = mapView
}
如您所见,整个事情不是动态的,而是静态的。我写下了我想要创建的标记数量。
有没有办法自动执行此操作?特别是当我不知道 json 文件中有多少信息以及应该创建多少标记时。
我现在解决了。
我试过了:
decodedData.results.forEach {
print([=10=].name)
print([=10=].place_id)
}
我做了一个 JSON 请求并获得了一些信息。
func parseJSON(poiData: Data) {
let decoder = JSONDecoder()
do {
let decodedData = try decoder.decode(POIData.self, from: poiData)
POIManager.POIname_One = decodedData.results[0].name
POIManager.POIplaceid_One = decodedData.results[4].place_id
POIManager.POIvicinity_One = decodedData.results[4].vicinity
POIManager.POIlong_One = decodedData.results[0].geometry.location.lat
POIManager.POIlat_One = decodedData.results[0].geometry.location.lng
POIManager.POIname_Two = decodedData.results[1].name
POIManager.POIplaceid_Two = decodedData.results[1].place_id
POIManager.POIvicinity_Two = decodedData.results[1].vicinity
POIManager.POIlong_Two = decodedData.results[1].geometry.location.lat
POIManager.POIlat_Two = decodedData.results[1].geometry.location.lng
POIManager.POIname_Three = decodedData.results[2].name
POIManager.POIplaceid_Three = decodedData.results[2].place_id
POIManager.POIvicinity_Three = decodedData.results[2].vicinity
POIManager.POIlong_Three = decodedData.results[2].geometry.location.lat
POIManager.POIlat_Three = decodedData.results[2].geometry.location.lng
} catch {
print(error)
}
}
在另一个 Swift 文件中,它将请求的结果放在如下列表中:
@IBAction func kategorieEins(_ sender: UIButton) {
//Eigene Standort soll hier gezeigt werden/aktualisierter Standort
locationManager.delegate=self
let marker1 = GMSMarker()
marker1.position = CLLocationCoordinate2D(latitude: POIManager.POIlong_One, longitude: POIManager.POIlat_One)
marker1.title = POIManager.POIname_One
marker1.snippet = "Marker1_0"
marker1.map = mapView
let marker2 = GMSMarker()
marker2.position = CLLocationCoordinate2D(latitude: POIManager.POIlong_Two, longitude: POIManager.POIlat_Two)
marker2.title = POIManager.POIname_Two
marker2.snippet = "Marker2_0"
marker2.map = mapView
let marker3 = GMSMarker()
marker3.position = CLLocationCoordinate2D(latitude: POIManager.POIlong_Three, longitude: POIManager.POIlat_Three)
marker3.title = POIManager.POIname_Three
marker3.snippet = "Marker3_0"
marker3.map = mapView
}
如您所见,整个事情不是动态的,而是静态的。我写下了我想要创建的标记数量。 有没有办法自动执行此操作?特别是当我不知道 json 文件中有多少信息以及应该创建多少标记时。
我现在解决了。 我试过了:
decodedData.results.forEach {
print([=10=].name)
print([=10=].place_id)
}