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

标签: json Swift
发布时间: 2017/3/5 5:20:28
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我正与 MapKit 示例使用 swift 。他们正在使用的 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'",

解决方法 1:

它看起来像你的示例使用SwiftyJSON你可以看看文档以获取有关如何获得一个 JSONValue 对象,该对象的数据的详细信息,但具体来说,如果我没有理解你的问题,你需要知道如何使用键,而不是位置的值。

好的它取决于什么值你越来越和是否这些值可以是 null 在 JSON 文件中 (aka 零在 Swift )

我要去报价你给的例子,然后向您展示如何从您的 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)
}
赞助商