I'm converting a project from Obj-c to Swift 3 and I can't get it to read JSON responses from our web methods. As far as I can see, the code looks like a good conversion from it's Obj-c counterpart, but the JSONSerilaization is having trouble.
I would post the old Obj-c, but it's spread across several NSURLConnection delegate methods. I can post this if required?
Swift 3:
// Set up the URL request
var getString : String = "https://TheWebMethod"
guard let url = URL(string: getString) else {
print("Error: cannot create URL")
return
}
let urlRequest = URLRequest(url: url)
// set up the session
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
// make the request
let task = session.dataTask(with: urlRequest) {
(data, response, error) in
// check for any errors
guard error == nil else {
print("error calling GET on Method")
print(error)
return
}
// make sure we got data
guard let responseData = data else {
print("Error: did not receive data")
return
}
// parse the result as JSON, since that's what the API provides
do {
guard let result = try JSONSerialization.jsonObject(with: responseData, options: []) as? [String: AnyObject] else {
print("error trying to convert data to JSON")
return
}
print("The result is: " + result.description)
guard let resultTitle = result["title"] as? String else {
print("Could not get title from JSON")
return
}
print("The title is: " + resultTitle)
} catch {
print("error trying to convert data to JSON")
return
}
}
task.resume()
Example JSON output when run through browser:
[{"id":"3","Initials":"TL","FullName":"Tony Law","LoginName":"test","Password":"password","EmailAddress":"myemailaddress","MobileTelNo":"0123456789","SecToken":"Secret"}]
Related
I'm trying to work with JSON data returned from a service. The JSON is, according to JSON validators, valid and is very simple:
[{"ID":"SDS-T589863","TotalRisk":0.2458,"TotalScore":641.032}]
However trying to parse it in my Swift 4 code it is mysteriously (to me at least) invalid. Here's my attempt to parse it:
// make the request
let task = session.dataTask(with: urlRequest) {
(data, response, error) in
// check for any errors
guard error == nil else {
print(error!)
return
}
// make sure we got data
guard let responseData = data else {
print("Error: did not receive data")
return
}
// this is fine:
guard let ddd = String(bytes: responseData, encoding: String.Encoding.utf8) else {
print("can't")
return
}
print(ddd) // prints [{"ID":"SDS-T589863","TotalRisk":0.2458,"TotalScore":641.032}] happily
do {
// cannot serialize
guard let risk = try JSONSerialization.jsonObject(with: responseData, options: [JSONSerialization.ReadingOptions.allowFragments])
as? [String: Any]
else {
print("error trying to convert data to JSON")
return
}
print(risk)
} catch {
print("error trying to convert data to JSON")
return
}
}
task.resume()
}
Assuming that I have no control over the JSON object or the format in which it is returned to me, is there a way to tell what is wrong with the JSON and perhaps format the response so that it can be serialized correctly?
You should cast your data to the [[String: Any]] type because you have array in response.
You are trying to cast to [String: Any], but you have an array of [String: Any] because your response enclosed in [] brackets.
Example:
let risk = try JSONSerialization.jsonObject(with: responseData, options: [JSONSerialization.ReadingOptions.allowFragments]) as? [[String: Any]]
Or if you want to get just only one [String: Any] object from response you can write:
let risk = (try JSONSerialization.jsonObject(with: responseData, options: [JSONSerialization.ReadingOptions.allowFragments]) as? [[String: Any]])?.first
Or if your object can be an array or not an array (but it sounds a little bit strange) you could try to cast to several possible types.
The response type is array of json objects so you have to cast it to [[String: Any]]. Since you are using Swift 4, you can use Decodable type which maps the model to the response.
let task = URLSession().dataTask(with: urlRequest) { (data, response, error) in
// check for any errors
guard error == nil else {
print(error!)
return
}
// make sure we got data
guard let responseData = data else {
print("Error: did not receive data")
return
}
do {
let decoder = JSONDecoder()
let riskArray = try decoder.decode([Risk].self, from: responseData)
print(riskArray)
} catch {
print("error trying to convert data to Model")
print(error.localizedDescription)
}
}
task.resume()
You can define your Model struct like
struct Risk: Decodable {
let id: String
let totalRisk: Double
let totalScore: Double
enum CodingKeys: String, CodingKey {
case id = "ID"
case totalRisk = "TotalRisk"
case totalScore = "TotalScore"
}
}
You can read more about Decodable protocol here
So I know how to parse JSON and retrieve a JSON from a URLRequest. What my objective is to remove this JSON file so I can manipulate it into different UIViewControllers. I have seen some stuff with completion handlers but I run into some issues, and I haven't fully understand. I feel like there is a simple answer, I am just being dumb.
How can I take this JSON outside the task and use it in other Swift files as a variable?
class ShuttleJson: UIViewController{
func getGenres(completionHandler: #escaping (_ genres: [String: Any]) -> ()) {
let urlstring = "_________"
let urlrequest = URLRequest(url: URL(string: urlstring)!)
let config = URLSessionConfiguration.default
let sessions = URLSession(configuration: config)
// request part
let task = sessions.dataTask(with: urlrequest) { (data, response, error) in
guard error == nil else {
print("error getting data")
print(error!)
return
}
guard let responseData = data else {
print("error, did not receive data")
return
}
do {
if let json = try JSONSerialization.jsonObject(with: responseData, options: []) as? [String: Any]{
//Something should happen here
}
print("no json sucks")
}
catch{
print("nah")
}
}
task.resume()
}
}
First of all remove the underscore and the parameter label from the completion handler. Both are useless
func getGenres(completionHandler: #escaping ([String: Any]) -> ()) {
Then replace the line
//Something should happen here
with
completionHandler(json)
and call the function
getGenres() { json in
print(json)
}
Notes:
The check guard let responseData = data else is redundant and it will never fail. If error is nil then data is guaranteed to have a value.
You should print the caught error rather than a meaningless literal string.
I'm trying to make this method accessible throughout the app because there are many view controllers need JSON response depending on the path and the language parameters, but I'm not sure what pattern to use or how to structure the app.
func fetchJsonFor(path: String, langugae: String) -> AnyObject{
var components = URLComponents()
components.scheme = Constants.APIScheme
components.host = Constants.APIHost
components.path = Constants.APIPath
components.path.append(path)
components.path.append(langugae)
let request = URLRequest(url: components.url!)
var parsedJSON: AnyObject!
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
if error != nil{
print(error?.localizedDescription ?? "Error")
return
}
guard let data = data else{
return
}
do{
parsedJSON = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as AnyObject
} catch{
print("Can't parse JSON: \(data)")
return
}
}
task.resume()
return parsedJSON
}
You can go for Single Tone Design pattern.
Also remember you can't return the URLRequest response as functions return. It is a asynchronous task which not works in main thread. So return will not work.
You need to make use of closure ----> a completion block will more suitable.
class WebService {
static let shared = WebService()
func fetchJsonFor(path: String, langugae: String,completion:((Any?) -> Void)){
var components = URLComponents()
components.scheme = Constants.APIScheme
components.host = Constants.APIHost
components.path = Constants.APIPath
components.path.append(path)
components.path.append(langugae)
let request = URLRequest(url: components.url!)
var parsedJSON: AnyObject!
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
if error != nil{
print(error?.localizedDescription ?? "Error")
completion(nil)
}
guard let data = data else{
completion(nil)
}
do{
parsedJSON = try JSONSerialization.jsonObject(with: data, options: .allowFragments)
completion(parsedJSON)
} catch{
print("Can't parse JSON: \(data)")
completion(nil)
}
}
task.resume()
}
}
How to use..
From your ViewController Class you can call web service like
WebService.shared.fetchJsonFor(path: "YOUR_PATH", langugae: "YOUR_LANGUAGE") { (response) in
if let response = response{
// Success response
}else{
//Failed response
}
}
I am using the JSONEncoder that has been provided with Swift4.
I have a class called Customer that uses the Codable protocol. Inside of Customer there are four Strings.
class Customer: Codable {
var title: String
var firstName: String
var lastName: String
var email: String
}
Reading JSON with a GET Request works fine.
URLSession.shared.dataTask(with: url) { (data, response, error) in
if error != nil {
print(error!.localizedDescription)
}
guard let data = data else { return }
do {
let customer = try JSONDecoder().decode(Customer.self, from: data)
DispatchQueue.main.async {
print("\(customer.title)")
print("\(customer.firstName)")
print("\(customer.lastName)")
print("\(customer.email)")
}
} catch let jsonError {
print(jsonError)
}
}.resume()
However when I start to do a POST request I am lost:
First set up the request object:
let urlString = "http://localhost:8000/customer"
guard let url = URL(string: urlString) else { return }
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("Application/json", forHTTPHeaderField: "Content-Type")
let customer = Customer()
customer.title = "Mr"
customer.firstName = "Chuck"
customer.lastName = "Norris"
customer.email = "chuck.norris#awsome.com"
let encodedData = try? JSONEncoder().encode(customer)
print(String(data: encodedData!, encoding: .utf8)!) //<- Looks as intended
// Output is {"firstName":"Chuck","lastName":"Norris","title":"MR","email":"chuck.norris#awesome.com "}
Now send it out
request.httpBody = encodedData //
URLSession.shared.dataTask(with: request, completionHandler: {(data, response, error) in
if let response = response {
print(response)
}
if let data = data {
do {
let json = try JSONSerialization.data(withJSONObject: data, options: [])
print(json)
}catch {
print(error)
}
}
})
In return I receive the message "The given data was not valid JSON."
So, my assumption is that I simply can not just put the encoded JSON data into the http body of my request.
Browsing through some articles about URLSession and JSON I found that it seems that I need to serialize my encoded JSON:
var json: Any?
if let data = encodedData {
json = try? JSONSerialization.jsonObject(with: data, options: .allowFragments)
}
Now, I don't know how to proceed. First I do not understand why I should serialize my JSON to something that can not simply put into a httpBody.
Because JSONSerialization.jsonObject produces Any? and not Data.
Update
Now, I was able to successfully send my data to my server. I am still trying to understand what was wrong - because I did not changed anything (except for the removal of the JSONSerialization call inside the completion Handler. I will investigate further...
...And .resume had been missing. :-)
I am trying to write a method that scan the barcode then using http rest call to get some JSON data from a server. Alamofire doesn't work now and I tried many different ways.
Here is what I got now:
let getEndpoint: String = "45.55.63.218:8080/food?foodidentifier=\(code)"
let requestURL: NSURL = NSURL(string: getEndpoint)!
let urlRequest: NSMutableURLRequest = NSMutableURLRequest(URL: requestURL)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(urlRequest) {
(data, response, error) -> Void in
let httpResponse = response as! NSHTTPURLResponse
let statusCode = httpResponse.statusCode
if (statusCode == 200) {
print("Everyone is fine, file downloaded successfully.")
do{
//let json = try NSJSONSerialization.JSONObjectWithData(data!, options:.AllowFragments)
}catch {
print("Error with Json: \(error)")
}
}
}
task.resume()
I get an error message:
fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb) on line: let httpResponse = response as! NSHTTPURLResponse
The problem is that the forced cast to NSHTTPURLResponse will fail if response was nil (for example, if there was some error that prevented the request from being issued successfully, as in this case where the URL string was missing the http:// scheme prefix; but this could happen for any of a variety of issues, so one must anticipate and handle network errors gracefully). When processing responses from network requests, one should studiously avoid forced unwrapping/casting unless you have first confirmed that the value is valid and non-nil.
I would suggest:
using guard statements to safely check for nil values;
check that data was not nil and that error was nil before bothering to check status codes;
So, that might yield:
let task = session.dataTaskWithRequest(urlRequest) { data, response, error in
guard data != nil && error == nil else {
print(error)
return
}
guard let httpResponse = response as? NSHTTPURLResponse where httpResponse.statusCode == 200 else {
print("status code not 200; \(response)")
return
}
print("Everyone is fine, file downloaded successfully.")
do {
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: [])
print(json)
} catch let parseError {
print("Error with Json: \(parseError)")
}
}
task.resume()