【问题标题】:Using JSON Decodable in Swift 4.1在 Swift 4.1 中使用 JSON 可解码
【发布时间】:2018-11-13 08:15:16
【问题描述】:
{  
    "count":30,
    "recipes":[  
        {  
            "publisher":"Closet Cooking",
            "f2f_url":"http://food2fork.com/view/35382",
            "title":"Jalapeno Popper Grilled Cheese Sandwich",
            "source_url":"http://www.closetcooking.com/2011/04/jalapeno-popper-grilled-cheese-sandwich.html",
            "recipe_id":"35382",
            "image_url":"http://static.food2fork.com/Jalapeno2BPopper2BGrilled2BCheese2BSandwich2B12B500fd186186.jpg",
            "social_rank":100.0,
            "publisher_url":"http://closetcooking.com"
        }
    ]
}

请问如何使用 Swift 4.1 Decodable 解析这个 JSON?

【问题讨论】:

  • 投反对票,因为 1) 不是 formatted 2) 没有显示尝试。
  • 试试app.quicktype.io,只要提供一个有效的json,你就会在这里得到模型。
  • 虽然有 M A N Y 帖子与此主题相关,但我假设您在询问之前尝试了一些东西...您能给我们看看吗:)
  • 我会确保下次显示尝试。感谢 cmets。
  • Jonathan:您可以通过自己的回顾性尝试并将其编辑到您的问题中来恢复您的部分分数。这样做肯定不会毫无意义,因为它会给你练习——如果你不熟悉这种语言,这很有用!

标签: json swift


【解决方案1】:

您的previous question 非常接近,但您必须为根对象添加结构

尽可能声明结构成员非可选。 URL 可以解码为URL

struct Root : Decodable {
    let count : Int 
    let recipes : [Recipe]
}

struct Recipe : Decodable { // It's highly recommended to declare Recipe in singular form
    let recipeId : String
    let imageUrl, sourceUrl, f2fUrl : URL
    let title : String
    let publisher : String
    let socialRank : Double
    let page : Int?
    let ingredients : [String]?
}

现在解码

let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let result = try decoder.decode(Root.self, from: data)
self.recipes = result.recipes

【讨论】:

  • 我完全按照您的描述进行操作,但仍然出现错误。无法解码 keyNotFound(CodingKeys(stringValue: "f2FURL", intValue: nil), @vadian
  • 还有 [CodingKeys(stringValue: "recipes", intValue: nil), _JSONKey(stringValue: "Index 0", intValue: 0)], debugDescription:
  • 区分大小写很重要:它是f2fUrl 第二个错误的消息是什么?
【解决方案2】:

以下是您的 JSON 模型:

struct Recipe: Codable{
    let publisher: String
    let f2f_url: String
    let title: String
    let source_url: String
    let recipe_id: String
    let image_url: String
    let social_rank: Float
    let publisher_url: String
}


struct  Model: Codable {
    let count: Int
    let recipes: [Recipe]
}

以下是可解码的 JSON:

let json = """
{
    "count":30,
    "recipes":[
        {
            "publisher":"Closet Cooking",
            "f2f_url":"http://food2fork.com/view/35382",
            "title":"Jalapeno Popper Grilled Cheese Sandwich",
            "source_url":"http://www.closetcooking.com/2011/04/jalapeno-popper-grilled-cheese-sandwich.html",
            "recipe_id":"35382",
            "image_url":"http://static.food2fork.com/Jalapeno2BPopper2BGrilled2BCheese2BSandwich2B12B500fd186186.jpg",
            "social_rank":100.0,
            "publisher_url":"http://closetcooking.com"
        }
    ]
}
""".data(using: .utf8)!

let decoder = JSONDecoder()

do {
   let model = try decoder.decode(Model.self, from: json) //Decode JSON Response Data
   print(model)
} catch let parsingError {
   print("Error", parsingError)
}

【讨论】:

  • 在 Swift 中命名变量时,我们通常不使用 snake case,因此您可能需要对其进行一些处理:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-28
  • 1970-01-01
  • 2018-12-06
  • 1970-01-01
  • 1970-01-01
  • 2020-08-15
相关资源
最近更新 更多