Parsing JSON issues with Swift using SwiftyJSON

I am new to swift and I am trying to parse some simple JSON data that I extract from the private API that I have. I am using the SwiftJSON library.

No matter what I do, I cannot assign the variable "videoUploadId" with the value "video_upload_id" coming from the JSON response. Hope I have provided enough information to get some help. Thanks

Here is a piece of code

let task = session.dataTaskWithRequest(request, completionHandler: { (data : NSData!, response, error : NSError!) -> Void in if (error == nil) { // Success let statusCode = (response as NSHTTPURLResponse).statusCode println("Status Code: \(statusCode)\r\n") println("Response: \(response)\r\n") println("Data: \(data)\r\n") let dataContent = NSString(data: data!, encoding: NSUTF8StringEncoding)! println("UTF8 Data: \(dataContent)\r\n") let json = JSON(dataContent) if let videoUploadId = json["video_upload_id"].int { println("Video Upload ID (Dict: int): \(videoUploadId)") } else if let videoUploadId = json["video_upload_id"].string { println("Video Upload ID (Dict: string): \(videoUploadId)") } else if let videoUploadId = json[0].int { println("Video Upload ID (Array: int): \(videoUploadId)") } else if let videoUploadId = json[0].string { println("Video Upload ID (Array: string): \(videoUploadId)") } else { println(json["video_upload_id"].error) } } else { // Failure println("URL Session Task Failed: %@", error.localizedDescription); } }) task.resume() 

This is what I get from my console:

Login: HTTP 200 Status code: 201

Answer: {URL: https: //// videos / uploads /} {status code: 201, headers {Connection = "Keep-alive"; "Content-Length" = 24; "Content-Type" = "application / json"; Date = "Sun, January 25, 2015 01:02:42 GMT"; Location = "https: //// videos / uploads /"; Server = "Apache / 2.2.15 (CentOS)"; "Set-Cookie" = "session = eyJzZXNzaW9uX3Rva2VuIjp7IiBiIjoiUzAxWGFYRnlVVGM1YjBsa1kxWkJiV2xrYVZwcFZXdDFiR0ZLYW5GQ1VqRjFjbk5GIn19.B6XSMg.HXatQ76ZFaoZEQsnNu1BgsVECKA; HttpOnly; Path = /"; }}

Data: <7b227669 64656f5f 75706c6f 61645f69 64223a20 3736307d>

UTF8 data: {"video_upload_id": 760}

Optional (Error Domain = SwiftyJSONErrorDomain Code = 901 "Dictionary error [" video_upload_id "], this is not a dictionary" UserInfo = 0x170238620 {NSLocalizedDescription = Dictionary ["video_upload_id"] failed, this is not a dictionary})

As you can see from the code and console output, I am trying to set a variable in several ways, all of which seem unsuccessful. I get the error "Dictionary error [" video_upload_id "], this is not the dictionary" I even tried doping "[" and adding "]" to try to find out if there is a formatting problem.

Any clues?

+5
source share
1 answer

You are doing the initialization incorrectly. You should use:

 let json = JSON(data:data) // data is NSData! 

Converting NSData to NSString is not required, and somehow wrong for that. SwiftyJSON can only be initialized with NSData or Swift.

+11
source

Source: https://habr.com/ru/post/1211856/


All Articles