我正在使用 swift 处理 MapKit 示例。他们使用的 JSON 对象是数组对象。我有一个 JSON 文件,它是对象数组。从示例中我看到他们正在通过数组中的位置提取他们想要的属性。我需要在我的对象中使用属性键。我该怎么做呢?第一次使用 swift。谢谢
这里是示例文件
init(title: String, locationName: String, discipline: String, coordinate: CLLocationCoordinate2D) {
self.title = title
self.locationName = locationName
self.discipline = discipline
self.coordinate = coordinate
super.init()
}
class func fromJSON(json: [JSONValue]) -> Artwork? {
// 1
var title: String
if let titleOrNil = json[16].string {
title = titleOrNil
} else {
title = ""
}
let locationName = json[12].string
let discipline = json[15].string
// 2
let latitude = (json[18].string! as NSString).doubleValue
let longitude = (json[19].string! as NSString).doubleValue
let coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
// 3
return Artwork(title: title, locationName: locationName!, discipline: discipline!, coordinate: coordinate)
}
var subtitle: String {
return locationName
}
// MARK: - MapKit related methods
// pinColor for disciplines: Sculpture, Plaque, Mural, Monument, other
func pinColor() -> MKPinAnnotationColor {
switch discipline {
case "Sculpture", "Plaque":
return .Red
case "Mural", "Monument":
return .Purple
default:
return .Green
}
}
// annotation callout opens this mapItem in Maps app
func mapItem() -> MKMapItem {
let addressDict = [String(kABPersonAddressStreetKey): self.subtitle]
let placemark = MKPlacemark(coordinate: self.coordinate, addressDictionary: addressDict)
let mapItem = MKMapItem(placemark: placemark)
mapItem.name = self.title
return mapItem
}
JSON 文件片段
[
198,
"9E5E1F28-22AF-459A-841E-5E89B022505E",
198,
1340413921,
"436621",
1340413921,
"436621",
"{\n}",
null,
null,
"1933",
"Bronze plaque mounted on a stone with an inscription marking the site of an artesian well. Located along Wilder Avenue near Artesian Way.",
"1922 Wilder Avenue",
"http://hiculturearts.pastperfect-online.com/34250images/004/193301-2.JPG",
"1933.01",
"Plaque",
"Site of Honolulu's Pioneer Artesian Well",
"Full",
"21.30006",
"-157.827969",
[
null,
"21.30006",
"-157.827969",
null,
false
],
null
],
我想使用的 JSON 文件片段
{
"id": "301B2",
"name": "Wrenhurst",
"lat": "35.815864",
"lng": "-78.918893",
"status": "Act 2Q12",
"minp": "632000",
"maxp": "678000",
"annStarts": "14",
"annClosings": "0",
"bldList": "Builder example",
"vdl": "0",
"futures": "0",
"lotSize": "95'",
最佳答案
看起来您的示例正在使用 SwiftyJSON 您可以查看文档以获取有关如何从 JSONValue 对象获取数据的更多信息,但具体来说,如果我理解您的问题,您需要知道如何使用键而不是位置获取值。
好的,这取决于您获取的值以及这些值在 JSON 文件中是否可以为 null(在 Swift 中也称为 nil)
我将引用您提供的示例,然后向您展示如何从 JSON 文件中获取该值
如果您知道 JSON 文件中的值永远不会为空并且始终会被提供:
// The code from your example using array position
let locationName = json[12].string
// Would convert to:
let locationName = json["name"].string
// And in the case of lat / lng
let latitude = (json[18].string! as NSString).doubleValue
// This converts to:
let latitude = (json["lat"].string! as NSString).doubleValue
只需使用键而不是位置即可获得您的值。然而,这并不是您需要的唯一改变。您不需要将 JSONValue 对象数组传递给工厂方法,而是需要指定一个普通的 JSONValue(或只是一个 JSON 对象)
所以你的工厂方法从此改变:
class func fromJSON(json: [JSONValue]) -> Artwork? {
// 1
var title: String
if let titleOrNil = json[16].string {
title = titleOrNil
} else {
title = ""
}
let locationName = json[12].string
let discipline = json[15].string
// 2
let latitude = (json[18].string! as NSString).doubleValue
let longitude = (json[19].string! as NSString).doubleValue
let coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
// 3
return Artwork(title: title, locationName: locationName!, discipline: discipline!, coordinate: coordinate)
}
对于这样的事情:
// This class method should return an Artwork object or nil if one couldn't be created for any reason.
// In your example, I couldn't find any reason you would return nil, so... in this example, it simply returns and Artwork object
class func fromJSON(json: JSON) -> Artwork {
// 1
var title: String
if let titleOrNil = json["title"].string {
title = titleOrNil
} else {
title = ""
}
let locationName = json["name"].string
let discipline = json["discipline"].string
// 2
let latitude = (json["lat"].string! as NSString).doubleValue
let longitude = (json["lng"].string! as NSString).doubleValue
let coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
// 3
return Artwork(title: title, locationName: locationName!, discipline: discipline!, coordinate: coordinate)
}
关于json - 如何使用 swift 与 MapKit 映射 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31924229/