json - Swift 4 Decodable - 动态类型、键和属性

标签 json swift decodable

我有一个返回 JSON 响应的标准 URL get 请求。标准的 JSON 响应是这样写的

//get request for the user
{
"USER":
    {
    "NAME":"myName",
    "ID":10000
    }
}
//get request for the forums
{  
"FORUM":[  
  {  
     "SORT":1,
     "ID":35,
     "TITLE":"Feedback",
     "SECTION":"secion1"
  },
  {  
     "SORT":2,
     "ID":92,
     "TITLE":"Choosing an ISP",
     "SECTION":"secion2"
  },
  {  
     "SORT":3,
     "ID":100,
     "TITLE":"Broadband",
     "SECTION":"section2"
  },
  {  
     "SORT":4,
     "ID":142,
     "TITLE":"“NBN”",
     "SECTION":"section2"
  },
 ]
}
//get request for news
{"NEWS":
  [
    {
      "DATE":"2018-10-13T03:56:06+1000",
      "SOURCE":"smh.com.au",
      "BLURB":"Assistant Treasurer Stuart Robert says he has repaid $37,975 of \"excess usage charges\" in home internet bills footed by taxpayers.",
      "ID":102347,
      "TITLE":"Stuart Robert pays back $38,000 in excessive home internet charges"
    },
    {
      "DATE":"2018-10-12T18:00:38+1000",
      "SOURCE":"itwire.com",
      "BLURB":"The CVC costs set by the NBN Co make it very difficult for ISPs to offer gigabit connections to more than a select band of customers who are willing to sign up in numbers and pay slightly more than other speed tiers, according to one ISP who caters to this type of consumer.",
      "ID":102343,
      "TITLE":"NBN gigabit connections will remain mostly a pipe dream"},
    {
      "DATE":"2018-10-12T09:48:43+1000",
      "SOURCE":"computerworld.com.au",
      "BLURB":"The Department of Home Affairs has rejects calls to include independent judicial oversight of the decision to issue Technical Assistance Notices and Technical Capability Notices as part of proposed legislation intended to tackle police agencies’ inability to access encrypted communications services.",
      "ID":102342,
      "TITLE":"Home Affairs rejects calls for additional safeguards in ‘spyware’ law"
    },
    {
    "DATE":"2018-10-11T12:16:05+1000",
    "SOURCE":"itnews.com.au",
    "BLURB":"NBN Co is hoping to “speed up” building works on the fibre-to-the-curb (FTTC) portion of its network as it tries to make up lost ground.",
    "ID":102334,
    "TITLE":"NBN Co says fibre-to-the-curb build is more complex that it hoped"
    },
  ]
}

正如我们所见,每个请求都有一个顶级 JSON 对象,有些请求可能有一个嵌套字典数组,而有些请求则没有。

问题

我在谷歌上搜索了几个小时,找到了 dynamic keyshere但不完全是我所追求的。由于类型保持不变。我想要某种能够在运行时识别的通用类型。一个简单的方法是执行 switch/if 语句,但我希望能够在没有太多维护和重复代码的情况下让它增长/缩小——我需要在每次发出不同的请求时重复 URLSession block .

URLSession.shared.dataTask(with: url) { (data, response, err) in
        guard let dataStr = data else {
            return
        }
        do {

            let json = try JSONSerialization.jsonObject(with: dataStr, options: [.mutableContainers, .allowFragments]) as! [String: Any]
            print("json - \(json)")

            let decoder = JSONDecoder()
            decoder.dateDecodingStrategy = .iso8601
            let root = try decoder.decode(RootUser.self, from: dataStr)// **Identify the Type.self during runtime without conditional statements** 
            print(root)


        } catch let jsonErr {
            print("error serializing json", jsonErr)
        }

        }.resume()

    URLSession.shared.dataTask(with: url) { (data, response, err) in
        //....
            let root = try decoder.decode(RootNews.self, from: dataStr)// **Identify the Type.self during runtime without conditional statements** 
       //...

        }.resume()

    URLSession.shared.dataTask(with: url) { (data, response, err) in
        //....
            let root = try decoder.decode(RootForum.self, from: dataStr)// **Identify the Type.self during runtime without conditional statements** 
       //...

        }.resume()

问题

我想知道如何在不使用条件的情况下将这些请求分组为通用方法

更新

我已经根据通用获取功能对已接受的答案进行了一些扩展。通过将我的结构(RootUser、RootNews)组合成一个结构

struct Base: Decodable {
let news: [News]?
let user: User?

enum CodingKeys: String, CodingKey {
    case news = "NEWS"
    case user = "USER"
}
}

现在当我调用 fetch 函数时,我只需要修改 URL 而不是类型(接受的答案中的 RootUser、RootNews 或 Foo),它将只是类型 Base。

最佳答案

不确定这是否回答了您的问题,但您可以使您的网络请求对任何 Decodable 通用。

    // Sample Model to be decoded
    struct Foo: Decodable {
        var id: String
        var name: String
    }

    // Generic function to fetch decodables
    func fetch<T: Decodable>(for url: URL, complete: @escaping (T?) -> Void) {
        URLSession.shared.dataTask(with: url) { (data, response, err) in
            do {
                let model = try JSONDecoder().decode(T.self, from: data!)
                complete(model)
            }
            catch {
                print(error)
                complete(nil)
            }
        }
    }

    // Test
    let request = URL(string: "some://url.com")
    fetch(for: request!) { (model: Foo?) -> Void in
        print("found \(model)")
    }

测试调用者知道它期望的是哪个模型,因此会在运行时推断并正确解码 Type Foo。

关于json - Swift 4 Decodable - 动态类型、键和属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52811181/

相关文章:

Javascript 函数即使找到对象也返回 false

php - 在php中必须从jquery [object Object]获取数据

ios - 如果特定的 View Controller 在屏幕上可见,如何从 View Controller 或类检查?或者我如何检查可见 View Controller ?

ios - 收到此错误 : nw_protocol_get_quic_image_block_invoke dlopen libquic failed

json - Swift,当 key 未知/动态时,如何使用 Decodable 和 Codable 解析/解码 JSON

php - SQl 连接整数表和整数 json 数组

jquery - 更改 json 值而不使用键名称

arrays - 在 swift 中初始化 json api 后出现错误

Ios 开发 : How often should an app check user is still logged in?

json - 可解码的 JSONDecoder 处理相同值的不同编码键