I'm building a simple app which fetches json data from server and present it to user.I have a json file in vapor Public directory and when I try to parse it in iOS app it gives an error data is in incorrect format.I have converted the json file to swift struct by using json to swift online convertor.
But when I test the server response with postman it gives me the json file.In iOS app it gives me an error.I was able to fetch the data if I print it it gives 1372 bytes but when I try to parse it gives me an error that data is incorrect format
I'm getting following error
typeMismatch(Swift.Dictionary<Swift.String, Any>, Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected to decode Dictionary<String, Any> but found an array instead.", underlyingError: nil))
my json file
{
"memes": [
{
"image": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-53-300x300.png",
"url": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-53.png"
},
{
"image": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-1-300x210.png",
"url": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-1.png"
},
{
"image": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-54-300x250.png",
"url": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-54.png"
},
{
"image": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-55-300x269.png",
"url": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-55.png"
},
{
"image": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-56.png",
"url": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-56.png"
},
{
"image": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-57.png",
"url": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-57.png"
},
{
"image": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-58.png",
"url": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-58.png"
},
{
"image": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-8.png",
"url": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-8.png"
},
{
"image": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-59.png",
"url": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-59.png"
},
{
"image": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-66.png",
"url": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-66.png"
},
{
"image": "https://www.testbytes.net/wp-content/uploads/2019/06/download.jpg",
"url": "https://www.testbytes.net/wp-content/uploads/2019/06/download.jpg"
},
{
"image": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-12-300x281.png",
"url": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-12.png"
},
{
"image": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-13-300x281.png",
"url": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-13.png"
},
{
"image": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-14-240x300.png",
"url": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-14.png"
},
{
"image": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-61.png",
"url": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-61.png"
},
{
"image": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-62.png",
"url": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-62.png"
},
{
"image": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-17-300x300.png",
"url": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-17.png"
},
{
"image": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-18-252x300.png",
"url": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-18.png"
},
{
"image": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-19-300x281.png",
"url": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-19.png"
},
{
"image": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-20-249x300.png",
"url": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-20.png"
},
{
"image": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-21-300x233.png",
"url": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-21.png"
},
{
"image": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-63.png",
"url": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-63.png"
},
{
"image": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-64.png",
"url": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-64.png"
},
{
"image": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-24-300x150.png",
"url": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-24.png"
},
{
"image": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-25-289x300.png",
"url": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-25.png"
},
{
"image": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-27.png",
"url": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-27.png"
},
{
"image": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-52-300x300.png",
"url": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-52.png"
},
{
"image": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-30-300x221.png",
"url": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-30.png"
},
{
"image": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-65.png",
"url": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-65.png"
},
{
"image": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-32-300x239.png",
"url": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-32.png"
},
{
"image": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-33-300x181.png",
"url": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-33.png"
},
{
"image": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-34-300x300.png",
"url": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-34.png"
},
{
"image": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-35-300x250.png",
"url": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-35.png"
},
{
"image": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-36-300x269.png",
"url": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-36.png"
},
{
"image": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-37-300x210.png",
"url": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-37.png"
},
{
"image": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-38-300x292.png",
"url": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-38.png"
},
{
"image": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-39-300x167.png",
"url": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-39.png"
},
{
"image": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-40-249x300.png",
"url": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-40.png"
},
{
"image": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-41-300x294.png",
"url": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-41.png"
},
{
"image": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-42-300x295.png",
"url": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-42.png"
},
{
"image": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-43-234x300.png",
"url": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-43.png"
},
{
"image": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-44-300x263.png",
"url": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-44.png"
},
{
"image": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-45-300x255.png",
"url": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-45.png"
},
{
"image": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-46-300x300.png",
"url": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-46.png"
},
{
"image": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-47.png",
"url": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-47.png"
},
{
"image": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-48-300x210.png",
"url": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-48.png"
},
{
"image": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-49-249x300.png",
"url": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-49.png"
},
{
"image": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-50-300x300.png",
"url": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-50.png"
},
{
"image": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-51-300x146.png",
"url": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-51.png"
},
{
"image": "https://www.probytes.net/wp-content/uploads/2018/05/python-2.jpg",
"url": "https://www.probytes.net/python-development-company/"
},
{
"image": "https://www.probytes.net/wp-content/uploads/2018/01/1.jpg",
"url": "https://www.probytes.net/wp-content/uploads/2018/01/1.jpg"
},
{
"image": "https://www.probytes.net/wp-content/uploads/2018/01/2.jpg",
"url": "https://www.probytes.net/wp-content/uploads/2018/01/2.jpg"
},
{
"image": "https://www.probytes.net/wp-content/uploads/2018/01/3.jpg",
"url": "https://www.probytes.net/wp-content/uploads/2018/01/3.jpg"
},
{
"image": "https://www.probytes.net/wp-content/uploads/2018/01/4-1.png",
"url": "https://www.probytes.net/wp-content/uploads/2018/01/4-1.png"
},
{
"image": "https://www.probytes.net/wp-content/uploads/2018/01/5-1.png",
"url": "https://www.probytes.net/wp-content/uploads/2018/01/5-1.png"
},
{
"image": "https://www.probytes.net/wp-content/uploads/2018/01/6-1.png",
"url": "https://www.probytes.net/wp-content/uploads/2018/01/6-1.png"
},
{
"image": "https://www.probytes.net/wp-content/uploads/2018/01/7-1.png",
"url": "https://www.probytes.net/wp-content/uploads/2018/01/7-1.png"
},
{
"image": "https://www.probytes.net/wp-content/uploads/2018/01/8-1.png",
"url": "https://www.probytes.net/wp-content/uploads/2018/01/8-1.png"
},
{
"image": "https://www.probytes.net/wp-content/uploads/2018/01/9-1.png",
"url": "https://www.probytes.net/wp-content/uploads/2018/01/9-1.png"
},
{
"image": "https://www.probytes.net/wp-content/uploads/2018/01/10-1.png",
"url": "https://www.probytes.net/wp-content/uploads/2018/01/10-1.png"
},
{
"image": "https://www.probytes.net/wp-content/uploads/2018/01/11-1.png",
"url": "https://www.probytes.net/wp-content/uploads/2018/01/11-1.png"
},
{
"image": "https://www.probytes.net/wp-content/uploads/2018/01/12-1.png",
"url": "https://www.probytes.net/wp-content/uploads/2018/01/12-1.png"
},
{
"image": "https://www.probytes.net/wp-content/uploads/2018/01/13-1.png",
"url": "https://www.probytes.net/wp-content/uploads/2018/01/13-1.png"
},
{
"image": "https://www.probytes.net/wp-content/uploads/2018/01/14-1.png",
"url": "https://www.probytes.net/wp-content/uploads/2018/01/14-1.png"
},
{
"image": "https://www.probytes.net/wp-content/uploads/2018/01/15-1.png",
"url": "https://www.probytes.net/wp-content/uploads/2018/01/15-1.png"
},
{
"image": "https://www.probytes.net/wp-content/uploads/2018/01/16.png",
"url": "https://www.probytes.net/wp-content/uploads/2018/01/16.png"
},
{
"image": "https://www.probytes.net/wp-content/uploads/2018/01/17.png",
"url": "https://www.probytes.net/wp-content/uploads/2018/01/17.png"
},
{
"image": "https://www.probytes.net/wp-content/uploads/2018/01/18.png",
"url": "https://www.probytes.net/wp-content/uploads/2018/01/18.png"
},
{
"image": "https://www.probytes.net/wp-content/uploads/2018/01/19.png",
"url": "https://www.probytes.net/wp-content/uploads/2018/01/19.png"
},
{
"image": "https://www.probytes.net/wp-content/uploads/2018/01/20.png",
"url": "https://www.probytes.net/wp-content/uploads/2018/01/20.png"
},
{
"image": "https://www.probytes.net/wp-content/uploads/2018/01/9-1.jpg",
"url": "https://www.probytes.net/wp-content/uploads/2018/01/9-1.jpg"
},
{
"image": "https://www.probytes.net/wp-content/uploads/2018/01/8-1.jpg",
"url": "https://www.probytes.net/wp-content/uploads/2018/01/8-1.jpg"
},
{
"image": "https://www.probytes.net/wp-content/uploads/2018/01/7.jpg",
"url": "https://www.probytes.net/wp-content/uploads/2018/01/7.jpg"
},
{
"image": "https://s.w.org/images/core/emoji/12.0.0-1/svg/1f609.svg"
},
{
"image": "https://www.probytes.net/wp-content/uploads/2018/01/3-2.jpg",
"url": "https://www.probytes.net/wp-content/uploads/2018/01/3-2.jpg"
},
{
"image": "https://www.probytes.net/wp-content/uploads/2018/01/6-1.jpg",
"url": "https://www.probytes.net/wp-content/uploads/2018/01/6-1.jpg"
},
{
"image": "https://www.probytes.net/wp-content/uploads/2018/01/Imgur-8801b2-1.png",
"url": "https://www.probytes.net/wp-content/uploads/2018/01/Imgur-8801b2-1.png"
},
{
"image": "https://www.probytes.net/wp-content/uploads/2018/01/5-1.jpg",
"url": "https://www.probytes.net/wp-content/uploads/2018/01/5-1.jpg"
},
{
"image": "https://www.probytes.net/wp-content/uploads/2018/01/r_389776_tqMPa-1.jpg",
"url": "https://www.probytes.net/wp-content/uploads/2018/01/r_389776_tqMPa-1.jpg"
},
{
"image": "https://www.probytes.net/wp-content/uploads/2018/01/2-2.jpg",
"url": "https://www.probytes.net/wp-content/uploads/2018/01/2-2.jpg"
},
{
"image": "https://www.probytes.net/wp-content/uploads/2018/01/4.jpg",
"url": "https://www.probytes.net/wp-content/uploads/2018/01/4.jpg"
}
]
}
Routes.swift file in Vapor
import Vapor
struct Welcome: Codable,Content {
let memes: [Meme]
}
struct Meme: Codable,Content {
let image: String
let url: String?
}
func routes(_ app: Application) throws {
var m = [Meme]()
var path = app.directory.publicDirectory
path.append("memes.json")
print(path)
guard let data = try? Data(contentsOf: URL(fileURLWithPath: path)) else {return }
do
{
let memess = try JSONDecoder().decode(Welcome.self, from: data)
m = memess.memes
}
catch{
print(error.localizedDescription)
}
app.get { req in
return m
}
}
iOS App codable struct file
import Foundation
public struct Json4Swift_Base : Codable {
public let memes : [Memes]?
public enum CodingKeys: String, CodingKey {
case memes = "memes"
}
public init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
memes = try values.decodeIfPresent([Memes].self, forKey: .memes)
}
}
public struct Memes : Codable {
public let image : String?
public let url : String?
public enum CodingKeys: String, CodingKey {
case image = "image"
case url = "url"
}
public init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
image = try values.decodeIfPresent(String.self, forKey: .image)
url = try values.decodeIfPresent(String.self, forKey: .url)
}
}
NetworkService file
import Foundation
public class NetworkService
{
public static let sharedobj = NetworkService()
public let url = URL(string:"http://127.0.0.1:8080")
public let session = URLSession(configuration: .default)
public func getMemes(onSucces:@escaping([Memes],Error?)->Void)
{
let task = session.dataTask(with: url!) { (data, response, error) in
do
{
let items = try JSONDecoder().decode(Json4Swift_Base.self, from: data!)
onSucces(items.memes!,error)
}
catch
{
print(error.localizedDescription)
}
}
task.resume()
}
}
In your Vapor code you decode the file and then extract only the array to a property so assuming that this is what you encode and send to the client then you only receive the array.
Like this
"[
{
"image": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-53-300x300.png",
"url": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-53.png"
},
{
"image": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-1-300x210.png",
"url": "https://www.testbytes.net/wp-content/uploads/2019/06/Untitled-1.png"
},
...
]"
So in your client you should decode the array only
let items = try JSONDecoder().decode([Memes].self, from: data!)
And you don't need init(from:)
or a CodingKey enum here.