简体   繁体   English

如何解码 Swift 中的 JSON 数据?

[英]How do I decode this JSON Data in Swift?

How do I decode this JSON Data?如何解码此 JSON 数据?

I've done it with the "drilling down" method where I kept calling each key and printing the value.我用“向下钻取”方法完成了它,我一直调用每个键并打印值。 I also tried the data model but it never worked.我也试过数据 model 但它从来没有用过。 I probably did something wrong but I don't know what.我可能做错了什么,但我不知道是什么。

Thanks in advance, If you need more information, just ask, I'm fairly new to Swift and Stackoverflow.提前致谢,如果您需要更多信息,请询问,我对 Swift 和 Stackoverflow 还很陌生。

   {
       "items":[
          {
             "id":16000014,
             "name":"BO",
             "starPowers":[
                {
                   "id":23000090,
                   "name":"CIRCLING EAGLE"
                },
                {
                   "id":23000148,
                   "name":"SNARE A BEAR"
                }
             ],
             "gadgets":[
                {
                   "id":23000263,
                   "name":"SUPER TOTEM"
                },
                {
                   "id":23000289,
                   "name":"TRIPWIRE"
                }
             ]
          },
          {
             "id":16000015,
             "name":"PIPER",
             "starPowers":[
                {
                   "id":23000091,
                   "name":"AMBUSH"
                },
                {
                   "id":23000152,
                   "name":"SNAPPY SNIPING"
                }
             ],
             "gadgets":[
                {
                   "id":23000268,
                   "name":"AUTO AIMER"
                },
                {
                   "id":23000291,
                   "name":"HOMEMADE RECIPE"
                }
             ]
          }
       ],
       "paging":{
          "cursors":{
    
          }
       }
    }

Decoding JSON in swift is insanely easy.在 swift 中解码 JSON 非常容易。 Just use the JSONDecoder class.只需使用JSONDecoder class。 Firstly, create a Codable class for your json response like this首先,像这样为您的 json 响应创建一个Codable class

struct Items: Codable {
    let items: [Item]
    let paging: Paging
}

struct Item: Codable {
    let id: Int
    let name: String
    let starPowers, gadgets: [Gadget]
}

struct Gadget: Codable {
    let id: Int
    let name: String
}

struct Paging: Codable {
    let cursors: Cursors
}

struct Cursors: Codable {
}
 

And then use it to parse your JSON like this然后用它像这样解析你的JSON

 let decoder = JSONDecoder()

do {
    let items = try decoder.decode(Items.self, from: jsonData)
    print(items)
    // Do something with the items here

} catch {
    print(error.localizedDescription)
}

struct Name {
    var id:Int
    var name:String
}

struct Cursor {
    // Your cursor model
}

struct Paging {
    var cursors: Cursor
}

struct Items {
    var id:Int
    var name:String
    var starPowers:[Name]
    var gadgets:[Name]
}

struct MainModel {
     var items : [Items]
     var paging : Paging
}

You can decode that data using let yourData = try. JSONDecoder().decode(MainModel,self: from: jsonData)您可以使用let yourData = try. JSONDecoder().decode(MainModel,self: from: jsonData) let yourData = try. JSONDecoder().decode(MainModel,self: from: jsonData) to get your desired JSON data. let yourData = try. JSONDecoder().decode(MainModel,self: from: jsonData)以获取您想要的 JSON 数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM