2016-09-29 57 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的” –