2016-07-08 10 views
0

私はちょうど良いことをするファイルをダウンロードしています。私はそれから価値を得るためにこのjsonを解析しようとしています。これがエラーの発生場所です。ファイルは私が思うようにうまくダウンロードします。ここではこれが私にswiftJsonでjson配列を解析しようとしています

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

を与えるalamofire何で私がこれまで持っているもの

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



    } 
} 

である私はこれを行う方法で行方不明ですか、私は完全に間違ってこれをやっているものがあります。 これは、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

関連する問題