1

I am downloading a file which does just fine. I am then trying to parse this json to get a value out from it. This is where the error occurs. The file downloads fine I think. Here is what I have so far

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



    }
}

This is what alamofire give me

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

Is there something I am missing on how to do this or am i doing this completely wrong. This is how you parse out a json array right?

Thank you for any help with this

2
  • What is your JSON? I could help you if you pasted the JSON response. Commented Jul 8, 2016 at 4:29
  • This is what it should give me [ { "video": "zKevpV_Qo7A" } ] Commented Jul 8, 2016 at 5:01

1 Answer 1

2

Use this code:

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you it took a bit or wrestling to get it to work properly but now my video is playing off the downloaded video, thank you a lot I appreciate it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.