Use JSONDecoder in swift 4 - arrays

I am using swift 4 xcode 9.2, I got the below error when using JSONDecoder.
typeMismatch(Swift.Array, Swift.DecodingError.Context(codingPath: [],
debugDescription: "Expected to decode Array but found a dictionary instead.",
underlyingError: nil))
class Hits: Codable {
let hits: [Hit]
init(hits: [Hit]) {
self.hits = hits
}
}
class Hit: Codable {
let recipe: String
let uri: String
let label: String
let image: String
let source: String
let url: String
let shareAs: String
let yield: String
init(recipe: String, uri: String, label: String, image: String, source: String, url: String, shareAs: String, yield: String) {
self.recipe = recipe
self.uri = uri
self.label = label
self.image = image
self.source = source
self.url = url
self.shareAs = shareAs
self.yield = yield
}
}
func downloadJSON() {
guard let downloadURL = url else {return}
URLSession.shared.dataTask(with: downloadURL) { (data, urlResponse, error) in
guard let data = data, error == nil, urlResponse != nil else { print("Something is wrong"); return }
print("download completed")
do {
let decoder = JSONDecoder()
let foods = try decoder.decode([Hits].self, from: data)
print(foods)
} catch {
print(error)
}
}.resume()
}
Here is the JSON:
https://api.edamam.com/search?q=chicken&app_id=110d8671&app_key=3f01522208d512f592625dfcd163ff5c&from=0&to=10

The error clears states that you are trying to decode an array but the actual type is a dictionary (single object).
Replace
let foods = try decoder.decode([Hits].self, from: data)
with
let foods = try decoder.decode(Hits.self, from: data)
And your classes (actually structs are sufficient) are
struct Recipe : Decodable {
let uri : URL
let label : String
let image : URL
let source : String
let url : URL
let shareAs : URL
let yield : Double
let calories, totalWeight, totalTime : Double
}
struct Hits: Decodable {
let hits: [Hit]
}
struct Hit: Decodable {
let recipe: Recipe
}

hits = try decoder.decode(Hits.self from: data)

Related

JSONObject of JSONArray with String values in Swift5

I need to create a JSON Object containing two JSON Arrays with a specific structure.
Here is the result I would need:
{"config": [{"battery_state" = "3.12","max_hum" = "33","mode" = "mode"}], "alarms": [{"1" = "12345678"}, {"2" = "22334455"}]}
I tried to use NSMutableDictionary and Arrays but the result is not what I'm expecting
let jsonConfigObject: NSMutableDictionary = NSMutableDictionary()
jsonConfigObject.setValue("33" as String, forKey: "max_hum" as String)
jsonConfigObject.setValue("3.12" as String, forKey: "battery_state" as String)
jsonConfigObject.setValue("mode" as String, forKey: "mode" as String)
let arrayConfig = [jsonConfigObject]
var jsonAlarmObject: NSMutableDictionary = NSMutableDictionary()
jsonAlarmObject.setValue("12345678" as String, forKey: "1" as String)
var arrayAlarms = [jsonAlarmObject]
jsonAlarmObject = NSMutableDictionary()
jsonAlarmObject.setValue("22334455" as String, forKey: "2" as String)
arrayAlarms.append(jsonAlarmObject)
let array = [["config" : arrayConfig], ["alarms" : arrayAlarms]]
The result is the following:
[["config": [{"battery_state" = "3.12";"max_hum" = 33;mode = mode;}]], ["alarms": [{1 = 12345678;}, {2 = 22334455;}]]]
any idea how I can get such JSON Structure ?
EDIT 1
I tried to use the following:
struct Config: Codable {
let battery_state, max_hum, mode: String
enum CodingKeys: String, CodingKey {
case battery_state = "battery_state"
case max_hum = "max_hum"
case mode = "mode"
}
}
var conf = Config(battery_state: "0", max_hum: "5", mode: "A")
var alarms = [[String:String]]()
alarms.append(["1":"123456"])
alarms.append(["2":"123456"])
alarms.append(["nb_alarms":String(Tag.sharedInstance.nb_alarms)])
but it gives me
{"config":[{"current_hum":"56","period":"2","battery_level":"2.9"],"alarms":[{"1":"123456"},{"2":"123456"},{"nb_alarms":"2"}]}
but I would need:
{"config":[{"current_hum":"56","period":"2","battery_level":"2.9"],"alarms":[{"1":"123456","2":"123456","nb_alarms":"2"]}
I need to change my alarm String:String but as I have plenty of data to be added in it I don't know the format of alarm I need to use...
Try this approach, using struct models (MyObject and Config) to ...create a JSON Object containing two JSON Arrays with a specific structure.
This example code shows how to code/decode your object from/to json.
struct MyObject: Codable {
let config: [Config]
let alarms: [[String: String]]
}
struct Config: Codable {
let batteryState, maxHum, mode: String
enum CodingKeys: String, CodingKey {
case batteryState = "battery_state"
case maxHum = "max_hum"
case mode
}
}
// the (corrected) json data
let json = """
{"config": [{"battery_state": "3.12","max_hum": "33","mode" : "mode"}], "alarms": [{"1": "12345678"}, {"2": "22334455"}]}
"""
// initial empty myObject
var myObject = MyObject(config: [], alarms: [])
// json string to MyObject
if let data = json.data(using: .utf8) {
do {
myObject = try JSONDecoder().decode(MyObject.self, from: data)
print("---> myObject: \(myObject)")
} catch {
print("decode error: \(error)")
}
}
// MyObject to json string
do {
let encodedData = try JSONEncoder().encode(myObject)
let theJson = String(data: encodedData, encoding: .utf8)
print("---> theJson: \(theJson!)")
} catch {
print(error)
}
// usage
myObject.config.forEach{ conf in
print("---> conf.batteryState: \(conf.batteryState)")
print("---> conf.maxHum: \(conf.maxHum)")
}
myObject.alarms.forEach{ alarm in
print("---> alarm: \(alarm)")
}

Decodeable SWIFT parsing Rest API data error

I am trying to query the marvel API. I believe my decodable is wrong. I have the following code:
struct ReturnedData : Decodable {
var id : Int?
var name : String?
var description : String?
}
var savedData : [ReturnedData] = []
let urlString = "https://gateway.marvel.com/v1/public/characters?ts=1&apikey=\(myAPIKey)"
let url = URL(string: urlString)
let session = URLSession.shared
let dataTask = session.dataTask(with: url!) { (data, response, error) in
guard let data = data else {return}
do {
let recievedData = try JSONDecoder().decode([ReturnedData].self, from: data)
self.savedData = recievedData
} catch {
print(error)
}
}
dataTask.resume()
}
I am getting the following error message:
typeMismatch(Swift.Array, Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected to decode Array but found a dictionary instead.", underlyingError: nil))
according to the documentation, I should get all of the below:
Character
id (int, optional): The unique ID of the character resource.,
name (string, optional): The name of the character.
description (string, optional): A short bio or description of the character.
modified (Date, optional): The date the resource was most recently modified.
resourceURI (string, optional): The canonical URL identifier for this resource.
urls (Array[Url], optional): A set of public web site URLs for the resource.
thumbnail (Image, optional): The representative image for this character.
comics (ComicList, optional): A resource list containing comics which feature this character.
stories (StoryList, optional): A resource list of stories in which this character appears
events (EventList, optional): A resource list of events in which this character appears.
series (SeriesList, optional): A resource list of series in which this character appears.
Also, any tips on how to get the thumbnail image is appreciated.
The error you get is because the model you have does not match the json data. So, try this approach ...to query the marvel API....
In future, copy and paste your json data into https://app.quicktype.io/
this will generate the models for you. Adjust the resulting code to your needs. Alternatively read the docs at: https://developer.marvel.com/docs#!/public/getCreatorCollection_get_0 and create the models by hand from the info given.
class MarvelModel: ObservableObject {
#Published var results = [MarvelResult]()
let myAPIKey = "xxxx"
let myhash = "xxxx"
func getMarvels() async {
guard let url = URL(string: "https://gateway.marvel.com/v1/public/characters?ts=1&apikey=\(myAPIKey)&hash=\(myhash)") else { return }
do {
let (data, _) = try await URLSession.shared.data(from: url)
let response = try JSONDecoder().decode(MarvelResponse.self, from: data)
Task{#MainActor in
results = response.data.results
}
} catch {
print("---> error: \(error)")
}
}
}
struct ContentView: View {
#StateObject var vm = MarvelModel()
var body: some View {
VStack {
if vm.results.isEmpty { ProgressView() }
List(vm.results) { item in
Text(item.name)
}
}
.task {
await vm.getMarvels()
}
}
}
struct MarvelResponse: Decodable {
let code: Int
let status, copyright, attributionText, attributionHTML: String
let etag: String
let data: MarvelData
}
struct MarvelData: Decodable {
let offset, limit, total, count: Int
let results: [MarvelResult]
}
struct MarvelResult: Identifiable, Decodable {
let id: Int
let name, resultDescription: String
let modified: String
let thumbnail: Thumbnail
let resourceURI: String
let comics, series: Comics
let stories: Stories
let events: Comics
let urls: [URLElement]
enum CodingKeys: String, CodingKey {
case id, name
case resultDescription = "description"
case modified, thumbnail, resourceURI, comics, series, stories, events, urls
}
}
struct Comics: Decodable {
let available: Int
let collectionURI: String
let items: [ComicsItem]
let returned: Int
}
struct ComicsItem: Identifiable, Decodable {
let id = UUID()
let resourceURI: String
let name: String
}
struct Stories: Decodable {
let available: Int
let collectionURI: String
let items: [StoriesItem]
let returned: Int
}
struct StoriesItem: Identifiable, Decodable {
let id = UUID()
let resourceURI: String
let name: String
let type: String
}
struct Thumbnail: Decodable {
let path: String
let thumbnailExtension: String
enum CodingKeys: String, CodingKey {
case path
case thumbnailExtension = "extension"
}
}
struct URLElement: Identifiable, Decodable {
let id = UUID()
let type: String
let url: String
}
EDIT-1: if you want something very basic, then try this:
struct ContentView: View {
#State var results = [MarvelResult]()
var body: some View {
List(results) { item in
Text(item.name)
}
.onAppear {
getMarvels()
}
}
func getMarvels() {
let myAPIKey = "xxxx"
let myhash = "xxxx"
guard let url = URL(string: "https://gateway.marvel.com/v1/public/characters?ts=1&apikey=\(myAPIKey)&hash=\(myhash)") else { return }
URLSession.shared.dataTask(with: url) { (data, _, _) in
if let data = data {
do {
let response = try JSONDecoder().decode(MarvelResponse.self, from: data)
DispatchQueue.main.async {
self.results = response.data.results
}
}
catch {
print(error)
}
}
}.resume()
}
}

parsing array of objects to array of string elements in swift JSON object

I am trying to parse the JSON object received from Web service which gives the result as JSON object of status and data.data again is a array of objects, from this object I want to take one element on the basis of which I have to fill a tableview.
web Service results comes as
{"status":1,"data":[{"service_id":"1","service_name":"Painter"},{"service_id":"2","service_name":"Plumber"},{"service_id":"3","service_name":"Electrician"},{"service_id":"4","service_name":"Handyman"},{"service_id":"5","service_name":"Carpenter"},{"service_id":"6","service_name":"Mason"}]}
parsing in swift I did as:--
created one class
class ABC: NSObject {
var service_name:String?
var service_id : Int?
init(service_name:String,service_id:Int) {
self.service_name = service_name
self.service_id = service_id
}
let myUrl = URL(string: "services.php");
var request = URLRequest(url:myUrl!)
request.httpMethod = "POST"// Compose a query string
let task = URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in
if error != nil
{
print("error=\(String(describing: error))")
return
}
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
if let parseJSON = json
{
let status=parseJSON["status"] as? Int
let newdata : NSDictionary = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSDictionary
self.model=(newdata.value(forKey: "data") as? [ABC])!
My problem is I am getting an array of objects in self.model as service_name and service_id keys.Now I want to take out one array of strings that contains all the service_name object values.Its saying not able to convert NSArray to swift array.
As already mentioned by others use (De)codable. It's easy to use and very comfortable. No type cast and no literal keys.
Create two structs, declare the members as non-optional constants with camelCased names and omit the initializer.
struct Root : Decodable {
let status : Int
let data : [Service]
}
struct Service : Decodable {
let serviceName : String
let serviceId : String
}
Then decode the JSON in another class or struct
let myUrl = URL(string: "services.php")
var request = URLRequest(url: myUrl!)
request.httpMethod = "POST"// Compose a query string
let task = URLSession.shared.dataTask(with: request) { data, _, error in
if let error = error { print(error); return }
do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let json = try decoder.decode(Root.self, from: data!)
let status = json.status
let newdata = json.data
} catch { print(error))
}
task.resume()
I would recommend to drop JSONSerialization and use Codable protocol instead with CodingKeys.
Here is a simple example to see how it works.
struct Service : Codable {
let id : Int
let name : String
// keys
private enum CodingKeys: String, CodingKey {
case id = "service_id"
case name = "service_name"
}
}
...
// assuming our data comes from server side
let jsonString = "..."
let jsonData = jsonString.data(using: .utf8)!
do {
let jsonDecoder = JSONDecoder()
let user = try jsonDecoder.decode(Service.self, from: jsonData)
print("Getting service: \(service.id) \(service.name)")
} catch {
print("Unexpected error: \(error).")
}
More documentation here.
Use native Swift type Dictionary everywhere you use NSDictionary now
Then get certain value for key by specifing key in dictionary's subscript
if let model = newData["data"] as? [ABC] {
self.model = model
}
Anyway, I would suggest you to start using Codable instead of JSONSerialization
struct Response: Decodable {
let status: Int
let data: [ABC]
}
struct ABC: Decodable {
let serviceName: String
let serviceId: String // note that `serviceId` isn’t `Int` But `String`
}
and then decode your data using JSONDecoder
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
do {
let response = try decoder.decode(Response.self, from: data!)
self.model = response.data
} catch { print(error) }

Error on Serialization

I'm getting this error when I try to proceed on parsing the JSON. Does anyone know what I gotta do to fix it?
Error 1: Cannot subscript a value of type 'AudiobookJSON' (aka 'Array<Dictionary<String, Any>>') with an index of type 'String'
Error on Print
File Model: Audiobook.swift:
import Foundation
struct Audiobook : Codable {
let id: Int
let descricao: String
let urlImagem: String
init(dictionary: AudiobookJSON) {
self.id = dictionary["id"] as! Int//////ERROR MESSAGE ////////
self.descricao = dictionary["descricao"] as! String/////ERROR MESSAGE
self.urlImagem = dictionary["urlImagem"] as! String////ERROR MESSAGE
}
}
Folder Networking: APIClient.swift:
import Foundation
typealias AudiobookJSON = [[String: Any]]
struct APIClient {
static func getAudiobooksAPI(completion: #escaping ([Audiobook]?) -> Void) {
let url = URL(string: "https://alodjinha.herokuapp.com/categoria")
let session = URLSession.shared
guard let unwrappedURL = url else { print("Error unwrapping URL"); return }
let dataTask = session.dataTask(with: unwrappedURL) { (data, response, error) in
guard let unwrappedDAta = data else { print("Error unwrapping data"); return }
do {
//let json = try JSONSerialization.jsonObject(with: unwrappedDAta, options: .allowFragments) as! [String:Any]
//let posts = json["data"] as? AudiobookJSON
let posts = try JSONDecoder().decode([Audiobook].self, from: unwrappedDAta)
print(posts)
completion(posts)
} catch {
print("Could not get API data. \(error), \(error.localizedDescription)")
}
}
dataTask.resume()
}
}
As I can see that AudiobookJSON is an array of key-value pairs that's why you are getting error: So you have to use codable like that:
First: you have to make Codable type struct like that(your codable struct variable names should be same as you are getting in response):
struct Audiobook: Codable {
let id: Int?
let descricao: String?
let urlImagem: String?
}
Second: when you get the response then parse directly using codale like that:
guard let unwrappedDAta = data else { print("Error unwrapping data"); return }
do {
let posts = try JSONDecoder().decode([Audiobook].self, from: unwrappedDAta)
print(posts)
completion(posts)
} catch let message {
print("JSON serialization error:" + "\(message)")
}
You can directly use the response like:
for audio in posts {
print("audio.id")
print("audio.descricao")
print("audio.urlImagem")
}

Swift 4 : Decode array

I want to easily decode a JSON file with Decode protocol from Swift 4 on Xcode 9. This my question :
How to decode à JSON like this:
[
{
"name": "My first Catalog",
"order": 0,
"products": [
{
"product": {
"title": "product title",
"reference": "ref"
}
}
]
}
]
I try this but it's doesn't work
fileprivate struct Catalog: Codable {
var name: String
var order: Int
var product: [Product]
}
fileprivate struct Product: Codable {
var title: String
var reference: String
}
...
// JSON Decoder
do {
let jsonData = try Data(contentsOf: URL(fileURLWithPath: filePath), options: .alwaysMapped)
let jsonDecoder = JSONDecoder()
let jsonCatalogs = try? jsonDecoder.decode(Array<Catalog>.self,
from: jsonData)
return jsonCatalogs
} catch {
print ("error")
return nil
}
I don't know why it doesn't work in Swift 4 with Xcode 9. Thank for your help ;-)
actually the thing is that your structs are wrong,
fileprivate struct Catalog: Codable {
var name: String
var order: Int
var products: [Products] // this key is wrong in your question, it should be products instead of product
}
//this particular structure was missing as your products is having a dictionary and in that dictionary you are having product at key product
fileprivate struct Products: Codable {
var product: Product
}
fileprivate struct Product: Codable {
var title: String
var reference: String
}
now you can check your fucntion and also you can easily debug it using try catch with error handlings
...
// JSON Decoder
do {
let jsonData = try Data(contentsOf: URL(fileURLWithPath:filePath), options: .alwaysMapped)
let jsonDecoder = JSONDecoder()
let jsonCatalogs = try? jsonDecoder.decode(Array<Catalog>.self,from: jsonData)
print(jsonCatalogs)
return jsonCatalogs
} catch let error {
print ("error -> \(error)") // this will always give you the exact reason for which you are getting error
return nil
}
Try this solution according to your JSON and i have worked with Decoder in swift 4 with xcode 9.4.
struct Catalog : Decodable {
let name : String?
let order : Int?
let productArray : [Products]? // this is products array.
}
struct Products : Decodable {
let productDict : Product? // this is product dictionary
}
struct Product : Decodable {
let title : String?
let reference : String?
}
var catalogArray = [Catalog]() // Declaration
// JSON Decoder
do {
let jsonData = try Data(contentsOf: URL(fileURLWithPath:filePath), options: .alwaysMapped)
let jsonDecoder = JSONDecoder()
let jsonCatalogs = try? jsonDecoder.decode(Catalog.self,from: jsonData)
return jsonCatalogs
} catch let error {
print ("error -> \(error)") // this will always give you the exact reason for which you are getting error
return nil
}
*

Resources