2016-07-08 38 views
0

我正在下載一個很好的文件。然後我試圖解析這個json以從中獲得價值。這是發生錯誤的地方。該文件下載罰款我想。以下是我迄今爲止試圖用swiftJson解析一個json數組

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() 
      } 



    } 
} 

這是alamofire給我

Success: true 
Response String: Optional("[ {\n \"video\" : \"zKevpV_Qo7A\"\n} ]") 

是有什麼我失蹤如何做到這一點還是我做這完全錯了。 這是如何解析出一個JSON數組的權利?

感謝您對本

+0

什麼是你的JSON?如果您粘貼了JSON響應,我可以幫助您。 – Dershowitz123

+0

這是它應該給我[ { 「視頻」:「zKevpV_Qo7A」 } ] – MNM

回答

2

使用任何幫助驗證碼:

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 
+0

謝謝你花了位或摔跤得到它的正常工作,但現在我的視頻播放關閉下載的視頻,謝謝我非常感謝你 – MNM