“我打算对Array <Any>进行解码,但找到了一个字典。”我正在尝试从json解析数据,但无法解决此问题

时间:2019-12-23 09:50:23

标签: json dictionary parsing

这是我的JSON

[
    {
        "id": 535,
        "name": "Canterbury Kings Vs Central Stags",
        "format": "t20",
        "series": 28,
        "seriesname": " SUPAR SMASH-MEN'S",
        "team1name": "CTB",
        "team2name": "CD",
        "matchkey": "sst20_2019_g11",
        "type": "cricket",
        "winnerstatus": "pending",
        "playing11_status": 0,
        "team1color": "#aaaaaa",
        "team2color": "#ffdc5e",
        "matchopenstatus": "opened",
        "time_start": "2019-12-23 11:30:00",
        "launch_status": "launched",
        "locktime": "2019-12-23 11:30:00",
        "status": true
    },
    {
        "id": 1058,
        "name": "Comilla Warriors Vs Dhaka Platoon",
        "format": "t20",
        "series": 27,
        "seriesname": "BPL T20",
        "team1name": "CAW",
        "team2name": "DHP",
        "matchkey": "bplt20_2020_g17",
        "type": "cricket",
        "winnerstatus": "pending",
        "playing11_status": 0,
        "team1color": "#eb6165",
        "team2color": "#4d84b8",
        "matchopenstatus": "opened",
        "time_start": "2019-12-23 13:00:00",
        "launch_status": "launched",
        "locktime": "2019-12-23 13:00:00",
        "status": true
    }
]

这是我的代码:

import UIKit

struct jsonData : Decodable{
    let team1name : String
    let team2name : String
    let name : String
    let team1logo : String
    let team2logo : String

    }

class homeVC:UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

//
        //加载视图后再进行其他设置。

    let jsonURL = "http://15.206.141.159/admin/api/getmatchlist"
    guard let url = URL(string: jsonURL) else{return}
    URLSession.shared.dataTask(with: url) {( data,response, err) in
        guard let data = data else{return}

        do{
            let root = try JSONDecoder().decode([jsonData].self, from: data)
            print(root)
         }

          catch let jsonErr{
            print("err",jsonErr)
        }
       }.resume()


}
}

0 个答案:

没有答案