简体   繁体   English

如何使用 SwiftyJSON 读取数组中的 JSON 数据?

[英]How would I read JSON data in an array using SwiftyJSON?

I have just been introduced to using JSON data in my iOS app.我刚刚被介绍在我的 iOS 应用程序中使用 JSON 数据。 I used a service to parse JSON data from a website, and I would like to decode that data to display to my user in a UITableView .我使用了一项服务来解析来自网站的 JSON 数据,并且我想解码该数据以在UITableView中显示给我的用户。 My JSON data looks as follows:我的 JSON 数据如下所示:

{
  "success": true,
  "outputScenario": "Default",
  "data": {
    "collection": [
      {
        "teamName": "Gulf Coast Monarchs, FL",
        "teamW": "10",
        "teamL": "0",
        "teamT": "0",
        "teamPct": "1.000",
        "teamGB": "-",
        "teamGP": "10",
        "teamRA": "10",
        "teamDivision": "10-0-0"
      },
      {
        "teamName": "Ohio Nationals, OH",
        "teamW": "9",
        "teamL": "1",
        "teamT": "0",
        "teamPct": ".900",
        "teamGB": "1.0",
        "teamGP": "10",
        "teamRA": "20",
        "teamDivision": "9-1-0"
      },      {
        "teamName": "Mount Kisco Chiefs, NY",
        "teamW": "0",
        "teamL": "8",
        "teamT": "0",
        "teamPct": ".000",
        "teamGB": "8.0",
        "teamGP": "8",
        "teamRA": "108",
        "teamDivision": "0-8-0"
      }
      {
        "teamName": "Mount Kisco Chiefs, NY",
        "teamW": "0",
        "teamL": "8",
        "teamT": "0",
        "teamPct": ".000",
        "teamGB": "8.0",
        "teamGP": "8",
        "teamRA": "108",
        "teamDivision": "0-8-0"
      }
    ]
  },

Just keep in mind that I have cut out a significant amount of the data provided in the JSON so it is easily viewable.请记住,我已经删除了 JSON 中提供的大量数据,因此很容易查看。

I would like to decode this data using SwiftyJSON if possible so I can display it to my user in a UITableView .如果可能,我想使用 SwiftyJSON 解码这些数据,以便我可以在UITableView其显示给我的用户。 For now, the UITableView will display the team name in the UITableView.textLabel.text and the teamW and teamL in the UITableView.detailTextLabel.text .目前,UITableView 将在UITableView.detailTextLabel.text中显示team name ,在UITableView.textLabel.text中显示teamWteamL How would I decode this data using SwiftyJSON?我将如何使用 SwiftyJSON 解码这些数据? I am struggling to figure out how this type of structure would be decoded.我正在努力弄清楚如何解码这种类型的结构。 I would like to use the model that I have created:我想使用我创建的 model:

struct Standing: Decodable {

var teamName: String
var teamW: Int
var teamL: Int
var teamT: Int
var teamPct: Int
teamGB: Int
teamGP: Int
teamRA: Int
teamDivision: String

}

If you're using Decodable , you don't need to use SwiftyJSON at all, everything's built into Swift itself.如果您使用的是Decodable ,则根本不需要使用SwiftyJSON ,所有内容都内置在 Swift 本身中。

Use this as your model struct:将此用作您的 model 结构:

struct Standing: Codable {
    let success: Bool
    let outputScenario: String
    let data: DataClass
}

struct DataClass: Codable {
    let collection: [Collection]
}

struct Collection: Codable {
    let teamName, teamW, teamL, teamT: String
    let teamPct, teamGB, teamGP, teamRA: String
    let teamDivision: String
}

and parse it like so:并像这样解析它:

do {
  let standing = try JSONDecoder().decode(Standing.self, from: data)
} catch {
  print(error)
}

Why do you want to use SwiftyJSON as you already adopted Decodable ?为什么要使用SwiftyJSON ,因为您已经采用了Decodable

The types in your struct are widely wrong because all values are String .您的结构中的类型广泛错误,因为所有值都是String And you need two other structs for the parent objects.你还需要另外两个结构作为父对象。

struct Root: Decodable {
    let success : Bool
    let outputScenario : String
    let data : TeamData
}

struct TeamData: Decodable {
    let collection : [Standing]
}

struct Standing: Decodable {
    let teamName, teamW, teamL, teamT: String
    let teamPct, teamGB, teamGP, teamRA: String
    let teamDivision: String
}

Decode the data, the Standing array is in the variable standings .解码数据, Standing数组在变量standings中。

do {
    let result = try JSONDecoder().decode(Root.self, from: data)
    let standings = result.data.collection
} catch {
    print(error)
}

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

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