如何使用 swift 使用 MapKit 映射 JSON 对象

how to map JSON object with MapKit using swift

我正在使用 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 文件中是否可以为空(也就是 Swift 中的 nil)

我将引用您提供的示例,然后向您展示如何从 JSON 文件中获取该值

如果您知道 JSON 文件中的某个值永远不会为 null 并且将始终提供:

// 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)
}