将数据从JSON Swift传递到数组

时间:2015-05-18 08:34:25

标签: json swift

我收到一个错误:“致命错误:当我尝试将JSON的结果传递给数组时,在展开Optional值时意外地发现了nil”。我认为问题是当我尝试获取地理坐标时:lat和long进入我的变量。 有人可以帮我解决这个问题吗? 将JSON传递给数组的代码是:

ViewController.swift

    import UIKit
    import MapKit
    import Alamofire

    class ViewController: UIViewController {

      var artworks = [Artwork]()

      @IBOutlet weak var mapView: MKMapView!

      let initialLocation = CLLocation(latitude: 52.370216, longitude: 4.895168)
      let regionRadius: CLLocationDistance = 6000

      override func viewDidLoad() {
        super.viewDidLoad()

    centerMapOnLocation(initialLocation)
    getData()
    mapView.addAnnotations(artworks)
    mapView.delegate = self


    }

    func getData() {
        Alamofire.request(.GET , "http://kaart.amsterdam.nl/datasets/datasets-item/t/kunstwerken-in-de-openbare-ruimte/export/json").responseJSON() {
          (_, _, json, _) in
          println(json)
          var json = JSON(json!)

          if let appArray = json["features"].array {

            for artObject in appArray {

              let title = artObject["properties"]["titel"].string
              let locationName = artObject["properties"]["titel_key"].string
              let discipline = artObject["properties"]["titel_key"].string


             let latitude = (artObject["geometry"]["coordinates"].string! as NSString).doubleValue
             let longitude = (artObject["geometry"]["coordinates"].string! as NSString).doubleValue


             let coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)

              var openArt1 = Artwork(title: title!, locationName: locationName!, discipline: discipline!, coordinate: coordinate)

              self.artworks.append(openArt1)
              self.mapView.addAnnotations(self.artworks)

          }

        }

      }

    }

      func centerMapOnLocation(location: CLLocation) {
        let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, regionRadius * 2.0, regionRadius * 2.0)
        mapView.setRegion(coordinateRegion, animated: true)

      }

the result of the JSON looks like this:

Optional({
    features =     (
                {
            geometry =             {
                coordinates =                 (
                    "4.9305202013246",
                    "52.363277804149"
                );
                type = Point;
            };
            properties =             {
                adres = "";
                content = "";
                "content_src" = "";
                "date_created" = "2015-05-16 00:07:58";
                "date_modified" = "<null>";
                datum = "2013-10-09 17:15:00";
                email = "";
                "id_2" = "";
                "link_href" = "";
                locatie = "POINT(4.93052020132461 52.363277804148602)";
                omschrijving = "";
                plaats = "";
                postcode = "";
                published = "";
                summary = "";
                "summary_type" = "";
                telefoonnummer = "";
                titel = "101 namen - 136602";
                "titel_key" = "101_namen_136602";
                title = "";
                type = Kunstwerk;
                updated = "";
                website = "http://www.amsterdam.nl/kunstencultuur/kunst-openbare/kunst-openbare/kunstwerken/amsterdam-stadsdeel-0/dapperbuurt/101-namen-136602/";
            };

1 个答案:

答案 0 :(得分:0)

artObject["geometry"]["coordinates"]是一个双打数组,而不是字符串。

如果您println JSON响应,可能会让您感到困惑,因为您看到双打包含在引号(")中但 仍然是JSON中的双打数组对于“坐标”键。

<强>更新

我刚检查过,他们是双打:

var err: NSError?
let data = NSData(contentsOfURL: NSURL(string: "http://kaart.amsterdam.nl/datasets/datasets-item/t/kunstwerken-in-de-openbare-ruimte/export/json")!)
let json = NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.allZeros, error: &err) as! [String:AnyObject]
let features = json["features"] as! [[String:AnyObject]]
let object1 = features[0]
let geo = object1["geometry"]?["coordinates"] as! [Double]
let lat = geo[0] // 4.9305202013246
let lon = geo[1] // 52.363277804149

您只需在循环中使用SwiftyJSON访问这些值:

 let coo = artObject["geometry"]["coordinates"].array!               
 let latitude = coo[0].doubleValue
 let longitude = coo[1].doubleValue