在字典中的数组中加载带字典的JSON文件

时间:2018-05-09 12:08:49

标签: arrays json swift dictionary loading

您好我正在尝试加载JSON并将其解析为另一个包含团队字典的数组。我成功地将每个团队名称加载到数组中,但是我无法完全了解如何在字典中包含包含ale的信息。

链接到JSON:https://statsapi.web.nhl.com/api/v1/standings

这就是我想要的数组

中的每个团队
team: {
id: 15,
name: "Washington Capitals",
link: "/api/v1/teams/15"
},
leagueRecord: {
wins: 49,
losses: 26,
ot: 7,
type: "league"
},
goalsAgainst: 239,
goalsScored: 259,
points: 105,
divisionRank: "1",
conferenceRank: "3",
leagueRank: "6",
wildCardRank: "0",
row: 46,
gamesPlayed: 82,
streak: {
streakType: "wins",
streakNumber: 1,
streakCode: "W1"
},
clinchIndicator: "y",
lastUpdated: "2018-05-08T00:46:01Z"
}

这是我加载和解析JSON的类

import UIKit

class StandingsTableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

var standingsURL: URL = URL(string: "https://statsapi.web.nhl.com/api/v1/standings")!

@IBOutlet weak var tableView: UITableView!
var standingData: [Records] = []
var standings = [String]()
let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .gray)

override func viewDidLoad() {
    super.viewDidLoad()
    loadStandings()
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 100
}

func loadStandings(){
    print("load standings")

    view.addSubview(activityIndicator)
    activityIndicator.frame = view.bounds
    activityIndicator.startAnimating()

    let standingsDatatask = URLSession.shared.dataTask(with: standingsURL, completionHandler: dataLoaded)

    standingsDatatask.resume()
}


func dataLoaded(data:Data?,response:URLResponse?,error:Error?){
    if let standingsDetailData = data{
        let decoder = JSONDecoder()
        do {
            let jsondata = try decoder.decode(Initial.self, from: standingsDetailData)
            var i = 0
            var j = 0
            standingData = jsondata.records!
            for _ in standingData{
                for _ in standingData[i].teamRecords {
                    standings.append(standingData[i].teamRecords[j].team.name)
                    j+=1
                    if(j >= standingData[i].teamRecords.count) {
                        j = 0
                    }
                }
                i+=1
            }
            print(standings[0])
                DispatchQueue.main.async{
                    self.tableView.reloadData()
                    self.activityIndicator.removeFromSuperview()
            }
        }catch let error{
            print(error)
        }
    }else{
        print(error!)
    }
}

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return standings.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "standingsCell", for: indexPath) as! standingsCell

    cell.teamImage.image = UIImage(named: standings[indexPath.row])
//        cell.gamesPlayed.text = String(standingData[indexPath.row].teamRecords[indexPath.row].gamesPlayed)
//        cell.wins.text = String(standingData[indexPath.row].teamRecords[indexPath.row].leagueRecord.wins)
//        cell.loses.text = String(standingData[indexPath.row].teamRecords[indexPath.row].leagueRecord.losses)
//        cell.overTime.text = String(standingData[indexPath.row].teamRecords[indexPath.row].leagueRecord.ot!)
//        cell.points.text = String(standingData[indexPath.row].teamRecords[indexPath.row].points)


        return cell
    }

}

这是我的dataModel

import Foundation

//Schedule

struct Initial: Codable {
    let totalGames: Int?
    let dates: [Dates]?
    let teams: [Teams]?
    let records: [Records]?
}


struct Dates: Codable {
    let date: String?
    let totalGames: Int
    let games: [Game]
}

struct Game: Codable {
    let link: String
    let gameDate: String
    let status: Status
    let teams: Team
    let venue: Venue
    let content: Content
}

struct Status: Codable {
    let abstractGameState: String
    let codedGameState: String
    let detailedState: String
    let statusCode: String
    let startTimeTBD: Bool
}

struct Team: Codable {
    let away: Away
    let home: Home
}

struct Away: Codable {
    let team: TeamInfo
}

struct Home: Codable {
    let team: TeamInfo
}

struct LeagueRecord: Codable {
    let wins: Int
    let losses: Int
    let type: String
    let ot: Int?
}

struct TeamInfo: Codable {
    let id: Int
    let name: String
    let link: String
}

struct Venue: Codable {
    let name: String
    let link: String
}

struct Content: Codable {
    let link: String
}

//teams

struct Teams: Codable {
    let id: Int
    let name: String
    let link: String
    let abbreviation: String
    let teamName: String
    let locationName: String
    let firstYearOfPlay: String?
    let officialSiteUrl: String
    let franchiseId: Int
    let venue: Venue
    let division: Division
    let conference: Conference
    let franchise: Franchise
}

struct Division: Codable {
    let id: Int
    let name: String
    let link: String
}

struct Conference: Codable {
    let id: Int
    let name: String
    let link: String
}

struct Franchise: Codable {
    let franchiseId: Int
    let link: String
}


//standings

struct Records: Codable {
    let division: Division
    let conference: Conference
    let teamRecords: [TeamRecords]
}

struct TeamRecords: Codable {
    let team: TeamInfo
    let leagueRecord: LeagueRecord
    let points: Int
    let gamesPlayed: Int
}

struct Standings: Codable {
    let teamRecords: [TeamRecords]
}

2 个答案:

答案 0 :(得分:1)

您可以像使用干净编码那样使用它standings现在拥有所有团队数据

这是请求和制图

Alamofire.request(URL.init(string: "https://statsapi.web.nhl.com/api/v1/standings")!)
                    .responseData { (data) in
                        if let initial = try? Initial.init(data: data.data!) , let record = initial.records{
                          let standings =   record.reduce([TeamRecord]()) { (result, record) -> [TeamRecord] in
                                return result + record.teamRecords
                            }
                            // Now standings have all Array you want
                        }
                    }

以下是具有可编码协议的模型

import Foundation

struct Initial: Codable {
    let copyright: String
    let records: [Record]?
}

struct Record: Codable {
    let standingsType: String
    let league, division, conference: Conference
    let teamRecords: [TeamRecord]
}

struct Conference: Codable {
    let id: Int
    let name, link: String
}

struct TeamRecord: Codable {
    let team: Conference
    let leagueRecord: LeagueRecord
    let goalsAgainst, goalsScored, points: Int
    let divisionRank, conferenceRank, leagueRank, wildCardRank: String
    let row, gamesPlayed: Int
    let streak: Streak
    let clinchIndicator: String?
    let lastUpdated: String
}

struct LeagueRecord: Codable {
    let wins, losses, ot: Int
    let type: TypeEnum
}

enum TypeEnum: String, Codable {
    case league = "league"
}

struct Streak: Codable {
    let streakType: StreakType
    let streakNumber: Int
    let streakCode: String
}

enum StreakType: String, Codable {
    case losses = "losses"
    case ot = "ot"
    case wins = "wins"
}


// MARK: Convenience initializers

extension Initial {
    init(data: Data) throws {
        self = try JSONDecoder().decode(Initial.self, from: data)
    }

    init(_ json: String, using encoding: String.Encoding = .utf8) throws {
        guard let data = json.data(using: encoding) else {
            throw NSError(domain: "JSONDecoding", code: 0, userInfo: nil)
        }
        try self.init(data: data)
    }

    func jsonData() throws -> Data {
        return try JSONEncoder().encode(self)
    }

    func jsonString(encoding: String.Encoding = .utf8) throws -> String? {
        return String(data: try self.jsonData(), encoding: encoding)
    }
}

答案 1 :(得分:0)

使用此方法解析API中的数据:

struct Stats: Codable {
    let copyright: String
    let records: [Record]
}

struct Record: Codable {
    let standingsType: String
    let league, division, conference: Conference
    let teamRecords: [TeamRecord]
}

struct Conference: Codable {
    let id: Int
    let name, link: String
}

struct TeamRecord: Codable {
    let team: Conference
    let leagueRecord: LeagueRecord
    let goalsAgainst, goalsScored, points: Int
    let divisionRank, conferenceRank, leagueRank, wildCardRank: String
    let row, gamesPlayed: Int
    let streak: Streak
    let clinchIndicator: String?
    let lastUpdated: String
}

struct LeagueRecord: Codable {
    let wins, losses, ot: Int
    let type: String
}

struct Streak: Codable {
    let streakType: StreakType
    let streakNumber: Int
    let streakCode: String
}

enum StreakType: String, Codable {
    case losses = "losses"
    case ot = "ot"
    case wins = "wins"
}