I am getting an error when trying to decode Json in swift 4
typeMismatch(Swift.Dictionary, Swift.DecodingError.Context(codingPath: [Swift._DictionaryCodingKey(stringValue: "ThrottleSeconds", intValue: nil)], debugDescription: "Expected to decode Dictionary but found a number instead.", underlyingError: nil))
Json reply is
{"Response":"4611686018456390681","ErrorCode":1,"ThrottleSeconds":0,"ErrorStatus":"Success","Message":"Ok","MessageData":{}}
struct BasicReply: Decodable {
let Response : String?
let ErrorCode : Int?
let ThrottleSeconds : Int?
let ErrorStatus : String?
let Message : String?
let MessageData : String?
}
class NetworkRequests {
let api = apiDetails()
func buildRequests()->String{
let request = "1/Stats/GetMembershipIdByDisplayName/" + api.userName + "/"
// let request = "1/Account/" + api.membershipId + "/Summary/"
return request
}
func request(){
let destinyURL : String = api.destinyURL + buildRequests()
let url = NSURL(string: destinyURL)
let request = NSMutableURLRequest(url: url! as URL)
request.httpMethod = "GET"
// add headers
request.setValue(api.apiKey, forHTTPHeaderField: "X-API-Key")
let session = URLSession.shared
let destinySession = session.dataTask(with: request as URLRequest) { (data, response, error) -> Void in
if let response = response as? HTTPURLResponse {
let code = response.statusCode
print(code)
}
guard let data = data else {print("No DATA!!!");return}
let output = String(data: data, encoding: String.Encoding.utf8)
print("output ", output! as Any)
do {
let questions = try JSONDecoder().decode([String: BasicReply].self, from: data)
} catch let jsonErr {
print("Error decoding Json Questons", jsonErr)
}
}
destinySession.resume()
}
}
Related
in postman response structure like this:
{
"categories": [
{
"id": 48,
"name": "Round-The-Clock",
"description": "24 hours round the clock menu",
"status": "enabled",
"products": [
{
"id": 280,
"name": ".Tea",.....
for this i have created Decodable model like this
struct Categories: Codable {
let categories: [Category]?
let featuredProducts: [Product]?
//coding keys..
}
struct Category: Codable {
let id: Int?
let name, categoryDescription: String?
let products: [Product]?
}
struct Product: Codable {
let id: Int?
let name, productDescription: String?
}
Parsing code: with this code break point hits with this if let jsonData = try? decoder.decode(Categories.self, from: respData) line but not hitting print("the categories are: (jsonData)") line and nothing comes in console, why? where am i wrong.. how to get response
class FoodMenuViewController: UIViewController {
private var catData: Categories? {
didSet{ }
}
func foodMenuServicecall(){
let urlStr = "http://54.149.84.126/categories?shop=1"
let url = URL(string: urlStr)
var req = URLRequest(url: url!)
req.httpMethod = "GET"
req.addValue("X-Requested-With", forHTTPHeaderField: "Content-Type")
req.addValue("XMLHttpRequest", forHTTPHeaderField: "X-Requested-With")
URLSession.shared.dataTask(with: url!, completionHandler: {(data, response, error) in
guard let respData = data else {return}
guard error == nil else {
print("error")
return
}
do{
let decoder = JSONDecoder()
if let jsonData = try? decoder.decode(Categories.self, from: respData) {
print("the categories are: \(jsonData)")
self.catData = jsonData
}
}
catch {print("catch error")}
}).resume()
}
EDIT: if i test like this i am getting response but here
func foodMenuServicecall(){
if let url = URL(string: "http://54.149.84.126//categories?shop=1"){
var req = URLRequest(url: url)
req.allHTTPHeaderFields = ["X-Requested-With" : "XMLHttpRequest"]
URLSession.shared.dataTask(with: req) { data, _, err in
guard let safeData = data else{return}
print(String(data: safeData, encoding: .utf8) ?? "")
}.resume()
}
}
o/p in consol:
Your decodable model expects a key of "categoryDescription", your JSON has a key of "description".
I am really getting stuck on this.
I have created a JSON service, that returns data like this:
[
{
"docNameField": "Test",
"docNumField": 22832048,
"docVerField": 1,
"docDataBaseField": "Legal",
"docCheckedOutWhenField": "03/05/2020",
"whereCheckedOutField": "PC0X8J9RD"
}
]
This is Postman output.
No matter how I try, I cannot seem to be able to put together the correct combination og HTTP call, deserialization, types and so on to get a list of objects out in the end.
This func below outputs this:
JSON String: Optional("[{\"docNameField\":\"Test\",\"docNumField\":22832048,\"docVerField\":1,\"docDataBaseField\":\"Legal\",\"docCheckedOutWhenField\":\"03/05/2020\",\"whereCheckedOutField\":\"PC0X8J9RD\"}]")
func LoadLockedDocumentsByDocnum(docNum:Int32) {
let json: [String: Any] = ["action":"getCheckedOutDocuments","adminUserName":"\(APPuserName)","adminPassword":"\(APPuserPassword)","adminDomain":"\(APPuserDomain)","applicationKey":"19730905{testKey}","searchTerm":docNum]
let jsonData = try? JSONSerialization.data(withJSONObject: json)
self.documentEntries.removeAll()
let url = URL(string: "https://{URL}//CheckOut")!
var request = URLRequest(url: url)
request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type") //Optional
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = jsonData
let session = URLSession.shared
let dataTask = session.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in
if let resultat = response as! HTTPURLResponse?{
if resultat.statusCode == 200{
if error != nil {
}
else {
print(data!)
if let nydata = data{
print("JSON String: \(String(data: data!, encoding: .utf8))")
}
}
}}
}
dataTask.resume()
}
You seem to have come pretty close. To get a list of objects out, you first need to declare that object:
struct MyResponseObject: Decodable { // please give this a better name
let docNameField: String
let docNumField: Int
let docVerField: Int
let docDataBaseField: String
let docCheckedOutWhenField: Date
let whereCheckedOutField: String
}
And then use a JSONDecoder to deserialise the JSON. Instead of:
print("JSON String: \(String(data: data!, encoding: .utf8))")
write:
let decoder = JSONDecoder()
let formatter = DateFormatter()
formatter.dateFormat = "MM/dd/yyyy"
decoder.dateDecodingStrategy = .formatted(formatter)
do {
// here's your list of objects!
let listOfObjects = try decoder.decode([MyResponseObject].self, from: data!)
} catch let error {
print(error) // an error occurred, you can do something about it here
}
I am trying to decode JSON. My swift function for decoding the JSON is:
func GetChapInfo(){
let endpoint = "https://chapel-logs.herokuapp.com/chapel"
let endpointUrl = URL(string: endpoint)
do {
var request = URLRequest(url: endpointUrl!)
request.httpMethod = "GET"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
let task = URLSession.shared.dataTask(with: request){
(data: Data?, response: URLResponse?, error: Error?) in
let dataAsString = String(data: data!, encoding: .utf8)
//print(dataAsString)
if(error != nil) {
print("Error")
}
else{
do{
guard let chapData = try? JSONDecoder().decode(Chapel.self, from: data!) else {
print("Error: Couldn't decode data into chapData")
return
}
for E in chapData.chap {
print(E.Day as Any)
}
}
}
}
task.resume()
}
}
my struct in Swift is
struct Chapel: Decodable {
let chap: [Chap]
}
struct Chap: Decodable {
let Name: String?
let Loc: String?
let Year: Int?
let Month: Int?
let Day: Int?
let Hour: Int?
let Min: Int?
let Sec: Int?
}
and my response from the server is:
{"chap":{"Name":"Why Chapel","Loc":"FEC","Year":2018,"Month":9,"Day":4,"Hour":16,"Min":1,"Sec":7}}
However when I run this the program prints out "Error: Couldn't decode data into chapData" and I cannot figure out why.
First of all catch decoding errors. Never try?. The caught error is much more descriptive
Expected to decode Array<Any> but found a dictionary instead
Means: The value for key chap is a dictionary, not an array
struct Chapel: Decodable {
let chap: Chap
}
And then you have to print
print(chapData.chap.Day)
You can reduce your code. An explicit URLRequest and headers for a default GET request is not needed. This is sufficient:
let endpoint = "https://chapel-logs.herokuapp.com/chapel"
let endpointUrl = URL(string: endpoint)!
do {
let task = URLSession.shared.dataTask(with: endpointUrl) { (data, response, error) in
...
I am getting response data nil.
func fetchSinglePageData() {
var response: NSData?
var errors: NSError?
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
response = NSData(contentsOfFile:"url...?project_id=(self.projectID)&project_page=Request", options: NSDataReadingOptions(0), error: &errors)
print("LatestURL Single page:(response)")
if response == nil {
var alertview = UIAlertView(title: "Network Error", message: "Data not received due to network connection.Try again...", delegate: self, cancelButtonTitle: "Ok")
alertview.show()
}
else{
let jsonDict: NSDictionary = NSJSONSerialization.JSONObjectWithData(response!, options: nil, error: &errors) as NSDictionary
print("JSON Response Single page:(jsonDict)")
var statuses = [NSObject : AnyObject]()
self.lblstartTime?.text = jsonDict["start_time"] as? String
self.lblcurrentStage?.text = jsonDict["current_stage"] as? String
self.lblcompletionTime?.text = jsonDict["completion_time"] as? String
self.lblManager?.text = jsonDict["manager"] as? String
}
})
}
project_id is string which is recevied from presvious page which is correctly working.
In swift 3
//make a url request
let urlString = "your_url"
let urlRequest = URLRequest.init(url: URL.init(string: urlString)!, cachePolicy: .reloadIgnoringCacheData, timeoutInterval: 50)
//create a session
let session = URLSession.shared
let task = session.dataTask(with: request) { (data, urlResponse, error) in
if error != nil{
//handle error
return
}else{
if let responseData = data{
let jsonDict = try JSONSerialization.jsonObject(with: responseData, options: [])as? [String:Any]
//handle your response here
}
}
}
task.resume()
In swift 2.2
let urlString = "your_url"
let request = NSURLRequest(URL: NSURL(string: urlString)!, cachePolicy: .ReloadIgnoringLocalCacheData, timeoutInterval: 50)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(request) { (data, response, error) in
if error != nil{
//handel error
print(error!.localizedDescription)
return
}
if let responseData = data{
do{
if let jsonDict = try NSJSONSerialization.JSONObjectWithData(responseData, options: [])as? [String:Any]{
//you have data in json dict
}
}catch{
print("error in parsing response")
return
}
}
}
task.resume()
func singlePageData(){
var errors: NSError?
let urlString = "xyz.com"
print("URL RESPONSE \(urlString)")
let request = NSURLRequest(URL: NSURL(string: urlString), cachePolicy: .ReloadIgnoringLocalCacheData, timeoutInterval: 50)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(request) { (data, response, error) in
if error != nil{
//handel error
print(error!.localizedDescription)
var alertview = UIAlertView(title: "Network Error", message: "Data not received due to network connection.Try again...", delegate: self, cancelButtonTitle: "Ok")
alertview.show()
return
}
if let responseData = data{
var jsonDict: NSDictionary = NSJSONSerialization.JSONObjectWithData(responseData, options: NSJSONReadingOptions.MutableContainers, error: &errors) as NSDictionary!
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
println("Json dict value \(jsonDict)")
self.lblprojectName?.text = jsonDict["name"] as? String
println("lblprojectName: \(self.lblprojectName?.text)")
self.lblstartTime?.text = jsonDict["start_time"] as? String
println("lblstartTime : \(self.lblstartTime?.text)")
self.lblcurrentStage?.text = jsonDict["current_stage"] as? String
println("lblcurrentStage : \(self.lblcurrentStage?.text)")
self.lblcompletionTime?.text = jsonDict["completion_time"] as? String
println("lblcompletionTime : \(self.lblcompletionTime?.text)")
self.lblManager?.text = jsonDict["manager"] as? String
})
}
}
task.resume()
}
This is my answer as per Jitendra Solanki answer i have made changes to code and it is now working in Swift 1.2
After a quick look, maybe \ missing:
NSData(contentsOfFile:"url...?project_id=\(self.projectID)&project_page=Request"
In this line:
response = NSData(contentsOfFile:"url...?project_id=(self.projectID)&project_page=Request", options: NSDataReadingOptions(0), error: &errors)
you have project_id=(self.projectID), to use interpolation you should instead have project_id=\(self.projectID)
I would suggest that you separate the code to generate your full URL, then print the URL to the console and ensure that it is what you would expect.
Then you can visit the url yourself and check to see what the server returns, then once you know you have the correct URL and response you can then work to deserialise the response
EDIT: updated with URLSession (Swift 3 version)
let defaultSession = URLSession(configuration: URLSessionConfiguration.default)
var dataTask: URLSessionDataTask?
let projectID = 12
let url = URL(string: "http://phpyouth.com/clients/halfpricedgrannyflats/app/app_response.php?project_id=\(projectID)&project_page=Request")
dataTask = defaultSession.dataTask(with: url!) {
data, response, error in
if let error = error {
print(error.localizedDescription)
} else if let httpResponse = response as? HTTPURLResponse {
if httpResponse.statusCode == 200 {
if let responseData = data {
if let json = try? JSONSerialization.jsonObject(with: responseData, options: .allowFragments) as? [String:AnyObject] {
print(json)
}
}
}
}
}
dataTask?.resume()
I'm having a problem with the following code. I'm downloading a list of actors in JSON and I want to populate Struct Actor with the received data. Everything works great until I try to flatMap on the received data and try to initialize the struct Actor. When I try to compile the code i get the error: Cannot assign value of type '()' to type [Actor]. The error corresponds to a line in viewDidLoad actorsList = downloadActors() Would anybody have any recommendation who to solve this?
import UIKit
func downloadActors() {
var request = URLRequest(url: URL(string: "url...")!)
request.httpMethod = "POST"
let postString = "actorGroup=\("Superhero")"
request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
DispatchQueue.main.async {
guard let data = data, error == nil else {
print("error=\(error)")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {
print("error : statusCode should be 200 but is \(httpStatus.statusCode)")
print("response = \(response)")
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode == 200 {
do {
let json = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers) as? [String: AnyObject]
guard let actorsJSON = json?["response"] as? [[String : AnyObject]] else {
return
}
} catch {
print("catch error")
}
}
}
}
task.resume()
}
func loadActors() -> [Actor] {
if let actors = actorsJSON as? [[String : AnyObject]] {
return actors.flatMap(Actor.init)
}
}
let actorsArray = loadActors()
class MasterViewController: UITableViewController {
var actorsList = [Actor]()
var detailViewController: DetailViewController? = nil
var objects = [Any]()
override func viewDidLoad() {
super.viewDidLoad()
actorsList = downloadActors()
print(actorsList)
Struct Actors is as follows:
struct Job {
let actorGroup: String
let actorName: String
}
extension Actor: JSONDecodable {
init?(JSON: [String : AnyObject]) {
guard let actorGroup = JSON["actorGroup"] as? String, let actorName = JSON["actorName"] as? String else {
return nil
}
self. actorGroup = actorGroup
self. actorName = actorName
}
}
let listActors = actorsJSON as? [[String : AnyObject]] {
Should be:
if let listActors = actorsJSON as? [[String : AnyObject]] {
Edit: For more info I'd like to add Vadian's comment:
Very confusing code. What does the function in the middle of the do block? Why do you type-check actorsJSON twice? The computed property is let listActors... which should be probably an optional binding (if let ... ). Further .mutableContainers is completely nonsense in Swift. And finally a JSON dictionary is [String:Any] in Swift 3.