【问题标题】:How would I read JSON data in an array using SwiftyJSON?如何使用 SwiftyJSON 读取数组中的 JSON 数据?
【发布时间】:2020-07-18 20:24:14
【问题描述】:

我刚刚被介绍在我的 iOS 应用程序中使用 JSON 数据。我使用了一项服务来解析来自网站的 JSON 数据,并且我想解码该数据以在 UITableView 中显示给我的用户。我的 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"
      }
    ]
  },

请记住,我已经删除了 JSON 中提供的大量数据,以便轻松查看。

如果可能,我想使用 SwiftyJSON 解码这些数据,以便我可以在 UITableView 中将其显示给我的用户。目前,UITableView 将在UITableView.textLabel.text 中显示team name,在UITableView.detailTextLabel.text 中显示teamWteamL。我将如何使用 SwiftyJSON 解码这些数据?我正在努力弄清楚如何解码这种类型的结构。我想使用我创建的模型:

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

}

【问题讨论】:

    标签: ios json uitableview swifty-json


    【解决方案1】:

    如果您使用的是Decodable,则根本不需要使用SwiftyJSON,一切都内置在 Swift 本身中。

    使用它作为你的模型结构:

    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
    }
    

    并像这样解析它:

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

    【讨论】:

    • 感谢您的回复。随着 vadian 的回答,我能够正确地构建我的代码。现在一切正常。
    【解决方案2】:

    既然你已经采用了Decodable,为什么还要使用SwiftyJSON

    你的结构体中的类型有很大的错误,因为所有的值都是String。您还需要另外两个结构作为父对象。

    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
    }
    

    解码数据,Standing数组在变量standings中。

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

    【讨论】:

    • 好的,在进行了这些重大更改后,一切都运行良好。我想我很难理解 JSON 的格式,而且我的实现很差。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-03
    相关资源
    最近更新 更多