2016-09-29 32 views
6

我是新的swift,我從請求中得到一個json,但我無法解析。我試圖讓JSON的信息並創建座標與註釋以及對mapkit使用快速解析json到一個數組3

下面是JSON我回去

{ 
    coord =  [ 
       { 
      islocationactive = 1; 
      latitude = "37.8037522"; 
      locationid = 1; 
      locationsubtitle = Danville; 
      locationtitle = "Schreiner's Home"; 
      longitude = "121.9871216"; 
     }, 
       { 
      islocationactive = 1; 
      latitude = "37.8191921"; 
      locationid = 2; 
      locationsubtitle = "Elementary School"; 
      locationtitle = Montair; 
      longitude = "-122.0071005"; 
     }, 
       { 
      islocationactive = 1; 
      latitude = "37.8186077"; 
      locationid = 3; 
      locationsubtitle = "Americas Eats"; 
      locationtitle = "Chaus Restaurant"; 
      longitude = "-121.999046"; 
     }, 
       { 
      islocationactive = 1; 
      latitude = "37.7789669"; 
      locationid = 4; 
      locationsubtitle = "Cheer & Dance"; 
      locationtitle = Valley; 
      longitude = "-121.9829908"; 
     } 
    ] } 

和我的代碼,試圖解析是這個

let task = URLSession.shared.dataTask(with: request as URLRequest){ 
      data, response, error in 

      //exiting if there is some error 
      if error != nil{ 
       print("error is \(error)") 
       return; 
      } 

      //parsing the response 
      do { 
       //converting resonse to NSDictionary 
       var teamJSON: NSDictionary! 

       teamJSON = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary 
       print(teamJSON) 
       //getting the JSON array teams from the response 
       let liquidLocations: NSArray = teamJSON["coord"] as! NSArray 

       //looping through all the json objects in the array teams 
       for i in 0 ..< liquidLocations.count{ 

        //getting the data at each index 
     //    let teamId:Int = liquidLocations[i]["locationid"] as! Int! 

       } 

      } catch { 
       print(error) 
      } 
     } 
     //executing the task 
     task.resume() 

但不是我嘗試工作。我想要得到的緯度,經度和地圖上創建一個annotationn

感謝您的幫助

+0

你需要告訴我們一些關於什麼是錯的。 –

+0

就行代碼 讓teamId:Int = liquidLocations [i] [「locationid」] as!詮釋!它拋出這個錯誤: 類型「任何」無標會員 –

回答

8

你可以用下面的代碼嘗試它作爲@Niko阿德里安斯相同Yuwono,但做了一些改變,所以你會得到隊伍整數

do { 
     let data : NSData = NSData() // change your data variable as you get from webservice response 
     guard let teamJSON = try NSJSONSerialization.JSONObjectWithData(data, options: []) as? [String: Any], 
      let liquidLocations = teamJSON["coord"] as? [[String: Any]] 
      else { return } 

     //looping through all the json objects in the array teams 
     for i in 0 ..< liquidLocations.count{ 
      let teamId: Int = (liquidLocations[i]["locationid"] as! NSString).integerValue 
      print(teamId) 
     } 

    } catch { 
     print(error) 
    } 
2

試試這個

 do { 
      guard let teamJSON = try JSONSerialization.jsonObject(with: data!, options: []) as? [String: Any], 
       let liquidLocations = teamJSON["coord"] as? [[String: Any]] 
       else { return } 

      //looping through all the json objects in the array teams 
      for i in 0 ..< liquidLocations.count{ 
       let teamId: Int = (liquidLocations[i]["locationid"] as! NSString).integerValue 
      } 

     } catch { 
      print(error) 
     } 

的關鍵是不使用的NSDictionary和NSArray的,因爲它不是strongly-類型(雖然你可以把它強類型太)使用SWIFT的陣列和字典,你可以使用[Element Type]數組和[Key: Value]的字典

+0

尼科嗨,當我運行代碼,我得到這個 無法投型「NSTaggedPointerString」(0x1027f1b90)的價值「的NSNumber」(0x101dfa300)。在這一行 讓teamId:Int = liquidLocations [i] [「locationid」]爲!詮釋! –

+0

@RodrigoSchreiner看起來像在Swift 3 id被翻譯成Any而不是AnyObject,你能測試我更新的答案嗎? –

+0

嗨尼科,我只是測試版本更新,我得到這個現在 無法投類型的值「NSTaggedPointerString」(0x10aef7b90)爲「NSNumber的」 –