中的iOS JSON解码器数据格式不正确错误
我正在构建一个简单的应用程序,从服务器获取json数据并将其呈现给用户。我在vapor Public directory中有一个json文件,当我尝试在iOS应用程序中解析它时,它会给出一个错误数据,数据格式不正确。我使用json到swift在线转换器将json文件转换为swift结构 但当我用postman测试服务器响应时,它会给我一个json文件。在iOS应用程序中,它会给我一个错误。如果我打印数据,它会给我1372字节,但当我尝试解析它时,它会给我一个错误,即数据格式不正确 我有以下错误 typeMismatchSwift.Dictionary,Swift.DecodingError.ContextcodingPath:[],调试说明:应解码字典,但找到了数组,UnderlineError:nil 我的json文件中的iOS JSON解码器数据格式不正确错误,ios,json,swift,vapor,Ios,Json,Swift,Vapor,我正在构建一个简单的应用程序,从服务器获取json数据并将其呈现给用户。我在vapor Public directory中有一个json文件,当我尝试在iOS应用程序中解析它时,它会给出一个错误数据,数据格式不正确。我使用json到swift在线转换器将json文件转换为swift结构 但当我用postman测试服务器响应时,它会给我一个json文件。在iOS应用程序中,它会给我一个错误。如果我打印数据,它会给我1372字节,但当我尝试解析它时,它会给我一个错误,即数据格式不正确 我有以下错误
{
"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"
}
]
}
Vapor中的Routes.swift文件
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应用程序可编码结构文件
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)
}
}
网络服务文件
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()
}
}
在Vapor代码中,您对文件进行解码,然后仅将数组提取到属性,因此假设这是您编码并发送给客户端的内容,那么您只接收数组 像这样
"[
{
"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"
},
...
]"
因此,在客户机中,您应该只解码阵列
let items = try JSONDecoder().decode([Memes].self, from: data!)
您不需要在此处使用initfrom:或CodingKey枚举。将printerror.localizedDescription更改为printerror,以获取更详细和有用的错误消息TypeMismatchSwift.Dictionary,Swift.DecodingError.ContextcodingPath:[],debugDescription:应解码字典,但找到了数组。,UnderlineError:JSON与结构匹配。错误表示根对象是数组。这是真正的JSON吗?顺便说一下,结构的大部分内容都是冗余的。例如,您可以将Json4Swift_Base简化为public struct Json4Swift_Base:Decodable{public let memes:[memes]}您可以免费获得codingkey和init方法else。