iOS JSON 解码器数据格式不正确错误

iOS JSON decoder data in not in correct format error

我正在构建一个简单的应用程序,它从服务器获取 json 数据并将其呈现给 user.I 在 vapor Public 目录中有一个 json 文件,当我尝试在 iOS 应用程序中解析它,它给出错误数据不正确 format.I 已将 json 文件转换为 swift 结构,方法是使用 json 到 swift在线转换器。

但是当我用邮递员测试服务器响应时,它给了我 json file.In iOS 应用程序,它给了我一个 error.I 能够获取数据,如果我打印它给出了 1372 个字节,但是当我尝试解析它时,它给我一个数据格式不正确的错误

我遇到以下错误

typeMismatch(Swift.Dictionary, Swift.DecodingError.Context(codingPath: [], debugDescription: "预期解码 Dictionary 但发现一个数组.", underlyingError: nil))

我的json文件

{
 "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 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 可编码结构文件

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!)

而且您不需要 init(from:) 或此处的 CodingKey 枚举。