Ios 试图用swiftJson解析json数组
我正在下载一个文件,它做得很好。然后我尝试解析这个json,从中获取一个值。这就是错误发生的地方。我想这个文件下载得很好。这是我到目前为止所拥有的Ios 试图用swiftJson解析json数组,ios,json,swift,swifty-json,Ios,Json,Swift,Swifty Json,我正在下载一个文件,它做得很好。然后我尝试解析这个json,从中获取一个值。这就是错误发生的地方。我想这个文件下载得很好。这是我到目前为止所拥有的 func parseYoutubeVideo(json : String) { if let data = json.dataUsingEncoding(NSUTF8StringEncoding) { let newJson = JSON(data: data) //Get the youtube
func parseYoutubeVideo(json : String)
{
if let data = json.dataUsingEncoding(NSUTF8StringEncoding)
{
let newJson = JSON(data: data)
//Get the youtube video from the Json data
print("Parsing Video")
self.myVideo = newJson["video"].stringValue
//load the video
print("Video parsed: " + self.myVideo)
self.myYoutubePlayer .loadWithVideoId(myVideo)
}
}
//Function to log into the server and retrive data
func downloadVideoUrl(myUrl : String)
{
print("Downloading video")
Alamofire.request(.GET, myUrl)
.authenticate(user: "admin", password: "admin")
.validate()
.responseString { response in
print("Success: \(response.result.isSuccess)")
print("Response String: \(response.result.value)")
if(response.result.isSuccess == true)
{
self.parseYoutubeVideo(response.result.value!)
//self.parseYoutubeVideo(self.testConfigJson)
}else
{
//remove player from the view
self.myYoutubePlayer.removeFromSuperview()
}
}
}
这是阿拉莫菲尔给我的
Success: true
Response String: Optional("[ {\n \"video\" : \"zKevpV_Qo7A\"\n} ]")
我在如何做这件事上有没有遗漏什么,或者我做得完全错了。
这就是解析json数组的方法,对吗
感谢您在此方面提供的帮助使用此代码:
let data = response.result.value!.dataUsingEncoding(NSUTF8StringEncoding)
var json: NSArray?
do {
json = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments) as! NSArray
}
catch error as NSError {
print(error)
}
let video = (json[0] as! NSDictionary)["video"] as String
你的JSON是什么?如果你粘贴了JSON响应,我可以帮你。这是它应该给我的[{“视频”:“ZKEVP_Qo7A”}]谢谢你让它正常工作花了点时间,但现在我的视频正在播放下载的视频,非常感谢