problem in Parsing json in SWIFT for iOS developing - ios

I have an Error in parsing JSON Format form an ASMX Web service,
My Code is
func getData() {
let url = URL(string: "http://192.168.11.188/getItems.asmx/theItems")
let theCategory = "ALL"
let theSubCategory = "ALL"
let postString = "theCategory=\(theCategory)&theSubCategory=\(theSubCategory)"
var request = URLRequest(url: url!)
request.httpMethod = "POST"
request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData
request.httpBody = postString.data(using: String.Encoding.utf8)
URLSession.shared.dataTask(with: request) { (data, respons, error) in
if let error = error {
print("Error conning to server \(error)")
} else {
if let respons = respons as? HTTPURLResponse {
if respons.statusCode == 200 {
print(data!)
if let data = data {
do {
let json = try JSONDecoder().decode([ITEMS].self, from: data)
print(json)
} catch let parsingError {
print("Error parsing json \(parsingError)")
}
}
} else {
print("Error in responce code.... \(respons.statusCode)")
}
}
}
}.resume()
}
I am using the decoder struct in this code:
struct ITEMS: Codable {
let CODE:String
let CAT_ID:String
let SUB_ID:String
let PRODUCT_AR:String
let PRODUCT_EN:String
let OLD_PRICE:String
let NEW_PRICE:String
let UNIT:String
let BARCODE:String
let THE_DATE:String
let TIME:String
}
The JSON value is
{ ITEMS : [{"CODE":111,"CAT_ID":203,"SUB_ID":null,"PRODUCT_AR":"ITEM 1","PRODUCT_EN":"ITEM 1","OLD_PRICE":133.0035,"NEW_PRICE":109,"UNIT":null,"BARCODE":"328031002009","THE_DATE":"\/Date(1553673958397)\/","TIME":"11:05 AM"},
{"CODE":222,"CAT_ID":201,"SUB_ID":null,"PRODUCT_AR":"ITEM 2","PRODUCT_EN":"ITEM 2","OLD_PRICE":18.95,"NEW_PRICE":9.95,"UNIT":null,"BARCODE":"628103400012","THE_DATE":"\/Date(1553673958260)\/","TIME":"11:05 AM"}]}
but this code returns an Error
Error parsing JSON Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
What am I doing wrong?

as you are writing this,
let json = try JSONDecoder().decode([ITEMS].self, from: data)
it must ask for array of ITEMS to be parsed, but your JSON is just an object not an array (It starts with {ITEMS: [array_here]}).
For solving this issue, you need to make your struct like below and parse data into that object.
struct ITEMS: Codable {
let CODE:String
let CAT_ID:String
let SUB_ID:String
let PRODUCT_AR:String
let PRODUCT_EN:String
let OLD_PRICE:String
let NEW_PRICE:String
let UNIT:String
let BARCODE:String
let THE_DATE:String
let TIME:String
}
struct MyAPIData: Codable {
let ITEMS: [ITEMS]
}
Now parse data using below line of code,
let json = try JSONDecoder().decode(MyAPIData.self, from: data)

There were wrong types used in your struct, here is the fixed struct
struct ITEM: Codable {
let CODE:Int // not String
let CAT_ID:Int // not String
let SUB_ID:Int? // its null in JSON, use either Int? or String?
let PRODUCT_AR: String
let PRODUCT_EN: String
let OLD_PRICE:Double // not String
let NEW_PRICE:Double // not String
let UNIT:String?
let BARCODE:String
let THE_DATE:String
let TIME:String
}
Since you JSON has a root element ITEMS, you need to decode using this struct
struct BaseItems: Codable {
let ITEMS: [ITEM] // Actual array of items are within this JSON element
}
Usage:
do {
let decoded = try JSONDecoder().decode(BaseItems.self, from: data)
print(decoded.ITEMS)
} catch {
print(error)
}

Related

Convert Alamofire Request to URLSession Request

I've this JSON response -
I've followed this blog to make api call and decode the response. Their code snippet for this -
func searchPlaces(query: String) {
let urlStr = "\(mapbox_api)\(query).json?access_token=\(mapbox_access_token)"
print(urlStr)
Alamofire.request(urlStr, method: .get, parameters: nil, encoding: URLEncoding.default, headers: nil).responseSwiftyJSON { (dataResponse) in
if dataResponse.result.isSuccess {
let resJson = JSON(dataResponse.result.value!)
if let myjson = resJson["features"].array {
for itemobj in myjson ?? [] {
try? print(itemobj.rawData())
do {
let place = try self.decoder.decode(Feature.self, from: itemobj.rawData())
self.searchedPlaces.add(place)
self.tableView.reloadData()
} catch let error {
if let error = error as? DecodingError {
print(error.errorDescription)
}
}
}
}
}
if dataResponse.result.isFailure {
let error : Error = dataResponse.result.error!
}
}
}
Here they take each features item by a for loop then decode that and append that in an array. I want to convert this by using URLSession. But this part they used dataResponse.result which is not available in URLSession -
if dataResponse.result.isSuccess {
let resJson = JSON(dataResponse.result.value!)
if let myjson = resJson["features"].array {
for itemobj in myjson ?? [] {
try? print(itemobj.rawData())
do {
let place = try self.decoder.decode(Feature.self, from: itemobj.rawData())
self.searchedPlaces.add(place)
self.tableView.reloadData()
}
So how can I convert this dataResponse.result part using URLSession?
Codable structs (This is they used for alamofire request) -
struct Feature: Codable {
var id: String!
var type: String?
var matching_place_name: String?
var place_name: String?
var geometry: Geometry
var center: [Double]
var properties: Properties
}
struct Geometry: Codable {
var type: String?
var coordinates: [Double]
}
struct Properties: Codable {
var address: String?
}
if you want to use URLSession this is a possible solution:
let session = URLSession.shared
let url = URL(string: "https://YOUR_URL")!
let task = session.dataTask(with: url, completionHandler: { data, response, error in
// Check the response
print(response)
// Check if an error occured
if error != nil {
// HERE you can manage the error
print(error)
return
}
// Serialize the data into an object
do {
let json = try JSONDecoder().decode(FullResponse.self, from: data!)
//try JSONSerialization.jsonObject(with: data!, options: [])
print(json)
DispatchQueue.main.async {
// pass data to main thread
}
} catch {
print("Error during JSON serialization: \(error.localizedDescription)")
}
})
task.resume()
You probably need to create a struct for the FullResponse.

Getting objects from JSON

My problem:
I use the site API - https://www.themealdb.com/api.php .
I want to get a list of all products. For this purpose, the link is https://www.themealdb.com/api/json/v1/1/categories.php
In my code, I created a structure:
struct Category: Decodable {
var idCategory: Int?
var strCategory: String?
var strCategoryDescription: String?
var strCategoryThumb: String?
}
Then I try to get to the address and get the data. I can convert the incoming data to JSON. It works.
Next, I want to convert the data and write it into an array of structures.
func load(url: String, completion: #escaping (_ objects: [Category])->()) {
guard let url = URL(string: url) else { return }
let session = URLSession.shared
session.dataTask(with: url) { (data, response, error) in
guard let data = data else { return }
do {
//let json = try? JSONSerialization.jsonObject(with: data, options: [])
//print("JSONSerialization" + "\(json)")
let object = try JSONDecoder().decode([Category].self, from: data)
print("JSONDecoder" + "\(object)")
completion(object)
} catch {
print(error.localizedDescription)
}
}.resume()
}
But in this line I get an error in the console:
The data couldn’t be read because it isn’t in the correct format.
Probably a mistake in my structure. I can not deal with this problem.
There are two mistakes.
The actual error
Type 'Array' mismatch: Expected to decode Array but found a dictionary instead.
indicates that you are ignoring the root object, the dictionary with key categories
The value for key id is String not Int, note the double quotes in the JSON
Declare all struct members as non-optional constants as the JSON provides all keys the in dictionaries. And please map the horrible dictionary keys to more meaningful member names.
And print all errors and never .localizedDescription in a Decodable catch block.
struct Response: Decodable {
let categories: [Category]
}
struct Category: Decodable {
let id: String
let name: String
let description: String
let thumbnailURL: URL
private enum CodingKeys: String, CodingKey {
case id = "idCategory"
case name = "strCategory"
case description = "strCategoryDescription"
case thumbnailURL = "strCategoryThumb"
}
}
func load(url: String, completion: #escaping ([Category]) -> Void) {
guard let url = URL(string: url) else { return }
let session = URLSession.shared
session.dataTask(with: url) { (data, _, error) in
if let error = error { print(error); return }
do {
let response = try JSONDecoder().decode(Response.self, from: data!)
print("JSONDecoder", response)
completion(response.categories)
} catch {
print(error)
completion([])
}
}.resume()
}
You need two codables
struct MyData: Codable {
var categories: [Category]?
}
And
let object = try JSONDecoder().decode(MyData.self, from: data)
With a wrapper class you can fetch your categories. The following code works fine in Playground:
let json = """
{
"categories": [
{"idCategory": "1"},
{"idCategory": "2"}
]
}
"""
struct CategoryHolder: Codable {
var categories: [Category]
}
struct Category: Codable {
let idCategory: String?
let strCategory: String?
let strCategoryDescription: String?
let strCategoryThumb: String?
}
let jsonData = Data(json.utf8)
let categories = try JSONDecoder().decode(CategoryHolder.self, from: jsonData).categories

Parsing JSON response using Codable gives error in swift

I'm trying to parse JSON response using Codable but it gives me error.
I tried to refer from the below stack overflow link but did not work.
how do I parse Any in dictionary using swift
Below is my code, not sure where I'm wrong in this.
> enum JSONError: String,Error {
> case NoData = "ERROR: no data"
> case ConversionFailed = "ERROR: conversion from JSON failed"
> }
struct Owner : Decodable {
let full_name : String
let html_url:String
let follower:follower
}
struct follower : Decodable {
let followers_url : String
}
func jsonParser() {
let urlPath = "https://api.github.com/search/repositories?q=language:ruby&sort=stars&order=desc"
guard let endpoint = NSURL(string: urlPath) else {
print("Error creating endpoint")
return
}
let request = NSMutableURLRequest(url:endpoint as URL)
URLSession.shared.dataTask(with: request as URLRequest) { (data, response, error) in
do {
guard let data = data else {
throw JSONError.NoData
}
let jsonResponse = try JSONSerialization.jsonObject(with:
data)
let entries = try! JSONDecoder().decode([Owner].self, from: jsonResponse as! Data)
print(jsonResponse)
} catch let error as JSONError {
print(error.rawValue)
} catch let error as NSError {
print(error.debugDescription)
}
}.resume()
}
I need to get 3 information from this response - full name, html url and followers.
Link for web api
https://api.github.com/search/repositories?q=language:ruby
Please latest have a look at the code.
Below is the error message :
'__NSDictionaryI' (0x102965a98) to 'NSData' (0x102964580). 2019-02-09
16:17:42.062971+0530 PhotoViewwer[13342:259997] Could not cast value
of type '__NSDictionaryI' (0x102965a98) to 'NSData' (0x102964580).
Thanks
Please learn to read JSON. It's pretty easy. There are only two collection types, array ([]) and dictionary ({})
Your structs are wrong.
In the root dictionary of the JSON there is an array of dictionaries for key items.
In each dictionary there are keys full_name and owner (here is the location of the Owner struct).
A dictionary for key follower does not exist.
These structs represent the JSON correctly
struct Response : Decodable {
let items : [Item]
}
struct Item : Decodable {
let fullName : String
let owner : Owner
}
struct Owner : Decodable {
let htmlUrl : URL // URL strings can be decoded directly into URL
let followersUrl : URL
}
Add a completion handler to your function and use an enum as result type. The failure case returns all real errors. An URLRequest is redundant. Just pass the URL. The JSONSerialization line is pointless.
The convertFromSnakeCase strategy converts snake_cased keys to camelCased struct members
enum Result {
case success(Response), failure(Error)
}
func jsonParser(completion: #escaping (Result) -> Void) {
let endpoint = URL(string:"https://api.github.com/search/repositories?q=language:ruby&sort=stars&order=desc")!
URLSession.shared.dataTask(with: endpoint) { (data, response, error) in
if let error = error { completion(.failure(error)); return }
do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let entries = try decoder.decode(Response.self, from: data!)
completion(.success(entries))
} catch {
completion(.failure(error))
}
}.resume()
}
And call it
jsonParser { result in
switch result {
case .success(let entries) : print(entries)
case .failure(let error) : print(error)
}
}
Basically never use NS... classes if there are native equivalents, here URL for NSURL and URLRequest for NS(Mutable)URLRequest
Edit:
In Swift 5 the syntax becomes more convenient using the native Result type. It is able to convert the throwing expression
enum Result {
case success(Response), failure(Error)
}
func jsonParser(completion: #escaping (Result<Response,Error>) -> Void) {
let endpoint = URL(string:"https://api.github.com/search/repositories?q=language:ruby&sort=stars&order=desc")!
URLSession.shared.dataTask(with: endpoint) { (data, response, error) in
if let error = error { completion(.failure(error)); return }
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
completion(Result{ try decoder.decode(Response.self, from: data!) })
}.resume()
}
You need
func jsonParser() {
let urlPath = "https://api.github.com/search/repositories?q=language:ruby&sort=stars&order=desc"
guard let endpoint = NSURL(string: urlPath) else {
print("Error creating endpoint")
return
}
let request = NSMutableURLRequest(url:endpoint as URL)
URLSession.shared.dataTask(with: request as URLRequest) { (data, response, error) in
do {
let dec = JSONDecoder()
dec.keyDecodingStrategy = .convertFromSnakeCase
let entries = try dec.decode(Root.self, from:data!)
print(entries)
} catch {
print(error)
}
}.resume()
}
struct Root : Decodable {
let items:[Item]
}
struct Owner: Codable {
let login: String
let id: Int
let nodeId: String
let avatarUrl: String
let gravatarId: String
let url, htmlUrl, followersUrl: String
let followingUrl, gistsUrl, starredUrl: String
let subscriptionsUrl, organizationsUrl, reposUrl: String
let eventsUrl: String
}
struct Item: Codable {
let fullName : String
let htmlUrl:String
let owner: Owner
}
You shouldn't cast the response to data here
from: jsonResponse as! Data)
as it will crash

Parsing the Json in tableview using Swift

I am new in parsing json.I made a single view application .I got json from url using this function .
func parseData(){
//created URL
guard let requestURL = URL(string: "https://machla.bh/api-category2") else {return }
//creating URLRequest
var request = URLRequest(url: requestURL)
//setting the method to post
request.httpMethod = "POST"
//creating the post parameter by concatenating the keys and values from text field
var postParameters = ""
postParameters += "key=LpfyirxoNOfP8wPns4nZqTw6DQ4wY A2q6yvpKsof6gkYDTykVXSEonTO2VB HE2zRdqrvsyoyMVyRagWtKAtVuOuNs c7QW5KrgbXS8SqPZ7sIDlPEvhBWyo5 NoObAcor3GlO87nRSaFdxhKDRTiBkK 3pFsTQyffzuBdIBiM8zFra6Yh8NbbC QQaratgFFE2hzLouNEIHq88xaSqum1 C0z7g325i3hixT5oLSo5tvhpvvdTJO WohfqGSakeGz7hsAU"
postParameters += "&path=59"
postParameters += "&language_id=1"
//adding the parameters to request body
request.httpBody = postParameters.data(using: .utf8)
//creating a task to send the post request
let session = URLSession.shared
let task = session.dataTask(with: request) { data, response, error in
guard error == nil else {
print("error is \(error!.localizedDescription)")
return
}
guard let data = data else {
print("No data was returned by the request!")
return
}
// print data from request
let str = String(data: data, encoding: .utf8)!
print(str)
for eachFetechedCountry in str
{
let eachCountry=eachFetechedCountry as! [String:Any]
let category = eachCountry["categories"] as! String
let product=eachCountry["products"] as! String
self.fetchcountry.append(Country(categories: category, products: product))
}
self.countryTableView.reloadData()
}
//executing the task
task.resume()
}
I am trying to read categories and products from json using following statement in above function.These statement are not working.How to read categories and products from json?
for eachFetechedCountry in str
{
let eachCountry=eachFetechedCountry as! [String:Any]
let category = eachCountry["categories"] as! String
let product=eachCountry["products"] as! String
self.fetchcountry.append(Country(categories: category, products: product))
}
For populating categories and product into table view i created a class named country
class Country{
var product:String
var category:String
init(categories :String, products:String)
{
self.category=categories
self.product=products
}
}
For populating categories and product into table view i coded
func tableView(_ tableViewr: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = countryTableView.dequeueReusableCell(withIdentifier: "cell")
cell?.textLabel?.text=fetchcountry[indexPath.row].category
cell?.detailTextLabel?.text=fetchcountry[indexPath.row].product
return cell!
}
how to populate categories and product into table view ?
From this link you can download sample project for correction?https://drive.google.com/file/d/0B5pNDpbvZ8SnY3RicXpGN1FYbXc/view?usp=sharing
You should use feature of Swift 4 - Decodable protocol.
let task = session.dataTask(with: request) { data, response, error in
guard error == nil else {
print("error is \(error!.localizedDescription)")
return
}
guard let data = data else {
print("No data was returned by the request!")
return
}
do {
let country = try JSONDecoder().decode(Country.self, from: data)
// do what you want here with Country array
// ...
} catch let error {
// catch error and handled it here
}
DispatchQueue.main.async {
self.countryTableView.reloadData()
}
}
//executing the task
task.resume()
Always update tableview in Main thread!!!
And use those structs:
struct Country: Decodable{
var categories: [Category]
}
struct Category: Decodable {
var name: String
var products: [Product]
}
struct Product: Decodable {
var name: String
}

Returning Data from JSON Parse - Swift

So I'm a newbie with Swift, and somewhat with programming in general. I've only been doing it since the first of the year... I'm trying to make a simple app that pulls data from an external JSON file, and inputs it into UILabels. I've gotten the data to pull, and append to an array. From here, it seems to exit the scope and not be useable anywhere else... I've created a struct that is to hold the data, accordingly. As you can see, I've added print markers to see what is going on, visually.
struct GlobalTestimonialData {
var testimonialsText: [String]
var customerNames: [String]
var companyNames: [String]
}
var TestimonialData = GlobalTestimonialData(testimonialsText: [""], customerNames: [""], companyNames: [""])
func getData () {
let requestURL: URL = URL(string: "https://szadydesigns.com/test/mobileapp/testimonials.php")!
let urlRequest = URLRequest(url: requestURL as URL)
let session = URLSession.shared
let task = session.dataTask(with: urlRequest, completionHandler: { (data, response, error) in
let httpResponse = response as! HTTPURLResponse
let statusCode = httpResponse.statusCode
if (statusCode == 200) {
print("File has been downloaded!")
do {
let json = try JSONSerialization.jsonObject(with: data!, options:.allowFragments)
print("JSON Serialized")
if let JSONfile = json as? [String: AnyObject] {
print("JSON Reading")
if let testimonial = JSONfile["testimonial"] as? [String] {
print("Testimonials Read")
TestimonialData.testimonialsText.append(contentsOf: testimonial)
print(TestimonialData.testimonialsText)
print("Inside of loop Testimonial Text Number: \(TestimonialData.testimonialsText.count)")
if let name = JSONfile["name"] as? [String] {
print("Names Read")
TestimonialData.customerNames.append(contentsOf: name)
print(TestimonialData.customerNames)
print("Inside of loop Customers Number: \(TestimonialData.customerNames.count)")
}
if let company = JSONfile["company"] as? [String] {
print("Companies Read")
TestimonialData.companyNames.append(contentsOf: company)
print(TestimonialData.companyNames)
}
print("Companies: \(TestimonialData.companyNames)")
}
print("COMPANIES: \(TestimonialData.companyNames)")
}
print("Companies AGIAN: \(TestimonialData.companyNames)")
}catch {
print("Error with Json: \(error)")
}
print("Companies AGIAN AGAIN : \(TestimonialData.companyNames)")
}
print("Companies AGIAN AGAIN AGAIN: \(TestimonialData.companyNames)")
}
//Loses Scope
print("Companies AGIAN TIMES : \(TestimonialData.companyNames)")
task.resume()
print("Outside of loop Customers Number: \(TestimonialData.customerNames.count)")
print("Outside of loop Testimonial Text Number: \(TestimonialData.testimonialsText.count)")
print(TestimonialData.companyNames)
}
I know I'm missing something really simple...but I'm at a loss... Any help/info is appreciated!
There are a few issues with this code:
First: The JSON you are receiving is not in the format your code expects. The root object isn't a dictionary but an array.
if let JSONfile = json as? [String: Any] {
changes to
if let JSONfile = json as? [[String: String]] {
This also requires you to loop over each item.
print("JSON Reading")
for item in JSONfile {
As the dictionary definition has changed from [String:Any] to [String:String] the as? String statements are no longer needed either.
Second: I'm not sure if you realise this or not (maybe you already do) but the bits of code after the //Loses Scope line run first. Before the Companies AGIAN AGAIN and Companies AGIAN AGAIN AGAIN lines.
They might be further down the page but the lines above that are in a closure that runs after the file has downloaded and so execute later after the other lines have already run.
Here is the complete fixed code (formatted in such a way you can copy and paste it into an Xcode Playground to see it working).
// Xcode 8.2, Swift 3
import Cocoa
import PlaygroundSupport
// Required for the download to work.
PlaygroundPage.current.needsIndefiniteExecution = true
struct GlobalTestimonialData {
var testimonialsText: [String]
var customerNames: [String]
var companyNames: [String]
}
var testimonialData = GlobalTestimonialData(testimonialsText: [], customerNames: [], companyNames: [])
func getData () {
let requestURL: NSURL = NSURL(string: "https://szadydesigns.com/test/mobileapp/testimonials.php")!
let urlRequest: NSMutableURLRequest = NSMutableURLRequest(url: requestURL as URL)
let session = URLSession.shared
let task = session.dataTask(with: urlRequest as URLRequest) { (data, response, error) in
let httpResponse = response as! HTTPURLResponse
let statusCode = httpResponse.statusCode
if (statusCode == 200) {
print("File has been downloaded!")
do {
let json = try JSONSerialization.jsonObject(with: data!, options:.allowFragments)
print("JSON Serialized")
if let JSONfile = json as? [[String: String]] {
print("JSON Reading")
for item in JSONfile {
if let testimonial = item["testimonial"] {
testimonialData.testimonialsText.append(testimonial)
if let name = item["name"] {
testimonialData.customerNames.append(name)
}
if let company = item["company"] {
testimonialData.companyNames.append(company)
}
}
}
}
} catch {
print("Error with Json: \(error)")
}
}
print("Companies Last: \(testimonialData.companyNames)")
print(" ")
print(testimonialData)
}
//Loses Scope
print("Companies 1 : \(testimonialData.companyNames)")
task.resume()
print("Before the download of the JSON Customer names count: \(testimonialData.customerNames.count)")
print("Before the download of the JSON Testimonial Text Number: \(testimonialData.testimonialsText.count)")
print(testimonialData.companyNames)
}
getData()
And you will get this output, which helps explain what is going on (though I removed the actual company names).
Companies 1 : []
Before the download of the JSON Customer names count: 0
Before the download of the JSON Testimonial Text Number: 0
[]
File has been downloaded!
JSON Serialized
JSON Reading
Companies: ["REMOVED_1", "REMOVED_2", "REMOVED_3", "REMOVED_4"]

Resources