如何使用swift处理JSON中丢失的对象?

时间:2019-03-02 08:14:09

标签: swift codable

我目前正在从Tracker Network Apex Legends Api获取数据。我在使用...时遇到了一个问题...

self.title2 = json.data.children.map{$0.stats[1].metadata.name}

将引发错误。据我所知,这是由于某些“孩子”没有价值。

简单地说,我想知道如何使用上面的代码,并用字符串(例如数组中的“ 0”或“ nil”)替换任何空值或不存在的值?

我将这些值放入数组中,然后在表格视图中显示它们。

由于每个实例都有值,因此以下方法可以很好地工作。

self.title2 = json.data.children.map{$0.stats[0].metadata.name}

这是我正在使用的以下代码。

 import Foundation

struct Store: Codable {
    let data: DataClass

    enum CodingKeys: String, CodingKey {
        case data = "data"
    }
}

struct DataClass: Codable {
    let id: String
    let type: String
    let children: [Child]
    let metadata: DataMetadata
    let stats: [Stat]

    enum CodingKeys: String, CodingKey {
        case id = "id"
        case type = "type"
        case children = "children"
        case metadata = "metadata"
        case stats = "stats"
    }
}

struct Child: Codable {
    let id: String
    let type: String
    let metadata: ChildMetadata
    let stats: [Stat]

    enum CodingKeys: String, CodingKey {
        case id = "id"
        case type = "type"
        case metadata = "metadata"
        case stats = "stats"
    }
}

struct ChildMetadata: Codable {
    let legendName: String
    let icon: String
    let bgimage: String

    enum CodingKeys: String, CodingKey {
        case legendName = "legend_name"
        case icon = "icon"
        case bgimage = "bgimage"
    }
}

struct Stat: Codable {
    let metadata: StatMetadata
    let value: Int
    let percentile: Double?
    let rank: Int?
    let displayValue: String
    let displayRank: String

    enum CodingKeys: String, CodingKey {
        case metadata = "metadata"
        case value = "value"
        case percentile = "percentile"
        case rank = "rank"
        case displayValue = "displayValue"
        case displayRank = "displayRank"
    }
}

struct StatMetadata: Codable {
    let key: String
    let name: String
    let categoryKey: StatsCategoryOrder
    let categoryName: CategoryName
    let isReversed: Bool

    enum CodingKeys: String, CodingKey {
        case key = "key"
        case name = "name"
        case categoryKey = "categoryKey"
        case categoryName = "categoryName"
        case isReversed = "isReversed"
    }
}

enum StatsCategoryOrder: String, Codable {
    case combat = "combat"
    case game = "game"
    case weapons = "weapons"
}

enum CategoryName: String, Codable {
    case combat = "Combat"
    case game = "Game"
    case weapons = "Weapons"
}

struct DataMetadata: Codable {
    let statsCategoryOrder: [StatsCategoryOrder]
    let platformID: Int
    let platformUserHandle: String
    let accountID: String
    let cacheExpireDate: String
    let level: Int

    enum CodingKeys: String, CodingKey {
        case statsCategoryOrder = "statsCategoryOrder"
        case platformID = "platformId"
        case platformUserHandle = "platformUserHandle"
        case accountID = "accountId"
        case cacheExpireDate = "cacheExpireDate"
        case level = "level"
    }
}

用于获取json数据并放入数组。

//images
var legendImgArr = [""]
//legend name
var legendTitle = [""]
//data set 1
var title1 = [""]
var data1 = [0]
//data set 2
var title2 = [""]
var data2 = [0]
//data set 3
var title3 = ["nil"]
var data3 = [0]

加载到UITableView中。

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

//loading Legend Images
    let request = NSURLRequest(url: NSURL(string: legendImgArr[indexPath.row])! as URL)

    URLSession.shared.dataTask(with: request as URLRequest) { (data, response, error) -> Void in

        if error != nil {
            print("Failed to load image for url: \(self.legendImgArr), error: ")
            return
        }

        guard let httpResponse = response as? HTTPURLResponse else {
            print("Not an NSHTTPURLResponse from loading url: \(self.legendImgArr)")
            return
        }

        if httpResponse.statusCode != 200 {
            print("Bad response statusCode: \(httpResponse.statusCode) while loading url: \(self.legendImgArr)")
            return
        }

        DispatchQueue.main.asyncAfter(deadline: .now() ) { () -> Void in
            cell.legendImage.image = UIImage(data: data!)
        }

        }.resume()

//loading labels
    cell.legendName.text = legendTitle[indexPath.row]


    cell.stat1Title.text = title1[indexPath.row]
    cell.stat1Data.text = String(data1[indexPath.row])

    cell.stat2Title.text = title2[indexPath.row]
    cell.stat2Data.text = String(data2[indexPath.row])

    cell.stat3Title.text = title3[indexPath.row]
    cell.stat3Data.text = String(data3[indexPath.row])

    return cell
}

获取Json数据。

let formattedName = usernameEntry.text!.replacingOccurrences(of: " ", with: "%20")

    let PlayerStatURL = URL(string: "https://public-api.tracker.gg/apex/v1/standard/profile/\(selectedPlatform)/\(formattedName)")

    if let unwrappedURL = PlayerStatURL {
        var request = URLRequest(url: unwrappedURL)
        request.addValue("my-api-key", forHTTPHeaderField: "TRN-Api-Key")
        let dataTask = URLSession.shared.dataTask(with: request) { (data, response, error) in
            // you should put in error handling code, too
            if let data = data {
                do {
                    let json = try JSONDecoder().decode(Store.self, from: data) as Store

                    // print(json.data)


                    //account level
                    print("Level: \(json.data.metadata.level)")

                    //images
                    self.legendImgArr = json.data.children.map{$0.metadata.icon}
                    //titles
                    self.legendTitle = json.data.children.map{$0.metadata.legendName}
                    //label 1 (this label works fine due to there being values for all 'legendName')
                    self.title1 = json.data.children.map{$0.stats[0].metadata.name}
                    self.data1 = json.data.children.map{$0.stats[0].value}


     //!!!Error happens here (I think this is because there are null values or nothing at all for some of them)!!!
                    self.title2 = json.data.children.map{$0.stats[1].metadata.name}
                    self.data2 = json.data.children.map{$0.stats[1].value}
                    self.title3 = json.data.children.map{$0.stats[2].metadata.name}
                    self.data3 = json.data.children.map{$0.stats[2].value}

                } catch {
                    print(error.localizedDescription)

                }
                DispatchQueue.main.asyncAfter(deadline: .now() ) {
                self.tableView.reloadData()
                self.viewDidLoad()
                self.statusOfLoad.stopAnimating()
                self.statusOfLoad.isHidden = true
                }
            }

        }
        dataTask.resume()


    }


}

我已将错误发生在上面的地方。 还知道这可行...

self.title2 = json.data.children[0].stats[1].metadata.name

但这不起作用

self.title2 = json.data.children.map{$0.stats[1].metadata.name}

这是有效代码的输出。

self.title1 = json.data.children.map{$0.stats[0].metadata.name}

输出:

["Kills", "Kills", "Kills", "Kills", "Kills", "Kills", "Kills", "Kills"]

这是无效代码的输出。

self.title2 = json.data.children.map{$0.stats[1].metadata.name}

输出:

Fatal error: Index out of range

Json,通过邮递员解析时。

{
"data": {
    "id": "b2553208-d89f-4b5d-b08d-5b61dc8d68fe",
    "type": "player",
    "children": [
        {
            "id": "legend_8",
            "type": "",
            "metadata": {
                "legend_name": "Pathfinder",
                "icon": "https://media.contentapi.ea.com/content/dam/apex-legends/images/2019/01/legends-character-tiles/apex-grid-tile-legends-pathfinder.png.adapt.crop16x9.png",
                "bgimage": "https://trackercdn.com/cdn/apex.tracker.gg/legends/pathfinder-concept-bg-small.jpg"
            },
            "stats": [
                {
                    "metadata": {
                        "key": "Kills",
                        "name": "Kills",
                        "categoryKey": "combat",
                        "categoryName": "Combat",
                        "isReversed": false
                    },
                    "value": 102,
                    "percentile": 17,
                    "rank": 8469,
                    "displayValue": "102",
                    "displayRank": "8,469"
                },
                {
                    "metadata": {
                        "key": "Damage",
                        "name": "Damage",
                        "categoryKey": "combat",
                        "categoryName": "Combat",
                        "isReversed": false
                    },
                    "value": 28845,
                    "percentile": 8,
                    "rank": 3455,
                    "displayValue": "28,845",
                    "displayRank": "3,455"
                },
                {
                    "metadata": {
                        "key": "Headshots",
                        "name": "Headshots",
                        "categoryKey": "combat",
                        "categoryName": "Combat",
                        "isReversed": false
                    },
                    "value": 163,
                    "percentile": 2.8,
                    "rank": 1363,
                    "displayValue": "163",
                    "displayRank": "1,363"
                }
            ]
        },
        {
            "id": "legend_6",
            "type": "",
            "metadata": {
                "legend_name": "Gibraltar",
                "icon": "https://media.contentapi.ea.com/content/dam/apex-legends/images/2019/01/legends-character-tiles/apex-grid-tile-legends-gibraltar.png.adapt.crop16x9.png",
                "bgimage": "https://trackercdn.com/cdn/apex.tracker.gg/legends/gibraltar-concept-bg-small.jpg"
            },
            "stats": [
                {
                    "metadata": {
                        "key": "Kills",
                        "name": "Kills",
                        "categoryKey": "combat",
                        "categoryName": "Combat",
                        "isReversed": false
                    },
                    "value": 1,
                    "percentile": 76,
                    "rank": 12420,
                    "displayValue": "1",
                    "displayRank": "12,420"
                },
                {
                    "metadata": {
                        "key": "Finishers",
                        "name": "Finishers",
                        "categoryKey": "game",
                        "categoryName": "Game",
                        "isReversed": false
                    },
                    "value": 0,
                    "rank": 256,
                    "displayValue": "0",
                    "displayRank": "256"
                }
            ]
        },
        {
            "id": "legend_2",
            "type": "",
            "metadata": {
                "legend_name": "Bangalore",
                "icon": "https://media.contentapi.ea.com/content/dam/apex-legends/images/2019/01/legends-character-tiles/apex-grid-tile-legends-bangalore.png.adapt.crop16x9.png",
                "bgimage": "https://trackercdn.com/cdn/apex.tracker.gg/legends/bangalore-concept-bg-small.jpg"
            },
            "stats": [
                {
                    "metadata": {
                        "key": "Kills",
                        "name": "Kills",
                        "categoryKey": "combat",
                        "categoryName": "Combat",
                        "isReversed": false
                    },
                    "value": 16,
                    "percentile": 76,
                    "displayValue": "16",
                    "displayRank": ""
                },
                {
                    "metadata": {
                        "key": "Headshots",
                        "name": "Headshots",
                        "categoryKey": "combat",
                        "categoryName": "Combat",
                        "isReversed": false
                    },
                    "value": 22,
                    "percentile": 100,
                    "rank": 14576,
                    "displayValue": "22",
                    "displayRank": "14,576"
                },
                {
                    "metadata": {
                        "key": "Specific2",
                        "name": "Legend Specific 2",
                        "categoryKey": "game",
                        "categoryName": "Game",
                        "isReversed": false
                    },
                    "value": 3,
                    "percentile": 100,
                    "rank": 2392,
                    "displayValue": "3",
                    "displayRank": "2,392"
                }
            ]
        },
        {
            "id": "legend_5",
            "type": "",
            "metadata": {
                "legend_name": "Bloodhound",
                "icon": "https://media.contentapi.ea.com/content/dam/apex-legends/images/2019/01/legends-character-tiles/apex-grid-tile-legends-bloodhound.png.adapt.crop16x9.png",
                "bgimage": "https://trackercdn.com/cdn/apex.tracker.gg/legends/bloodhound-concept-bg-small.jpg"
            },
            "stats": [
                {
                    "metadata": {
                        "key": "Kills",
                        "name": "Kills",
                        "categoryKey": "combat",
                        "categoryName": "Combat",
                        "isReversed": false
                    },
                    "value": 6,
                    "percentile": 83,
                    "displayValue": "6",
                    "displayRank": ""
                },
                {
                    "metadata": {
                        "key": "Specific2",
                        "name": "Legend Specific 2",
                        "categoryKey": "game",
                        "categoryName": "Game",
                        "isReversed": false
                    },
                    "value": 4,
                    "percentile": 100,
                    "rank": 4007,
                    "displayValue": "4",
                    "displayRank": "4,007"
                }
            ]
        },
        {
            "id": "legend_1",
            "type": "",
            "metadata": {
                "legend_name": "Wraith",
                "icon": "https://media.contentapi.ea.com/content/dam/apex-legends/images/2019/01/legends-character-tiles/apex-grid-tile-legends-wraith.png.adapt.crop16x9.png",
                "bgimage": "https://trackercdn.com/cdn/apex.tracker.gg/legends/wraith-concept-bg-small.jpg"
            },
            "stats": [
                {
                    "metadata": {
                        "key": "Kills",
                        "name": "Kills",
                        "categoryKey": "combat",
                        "categoryName": "Combat",
                        "isReversed": false
                    },
                    "value": 7,
                    "percentile": 87,
                    "displayValue": "7",
                    "displayRank": ""
                },
                {
                    "metadata": {
                        "key": "ArKills",
                        "name": "AR Kills",
                        "categoryKey": "weapons",
                        "categoryName": "Weapons",
                        "isReversed": false
                    },
                    "value": 0,
                    "rank": 1478,
                    "displayValue": "0",
                    "displayRank": "1,478"
                }
            ]
        },
        {
            "id": "legend_4",
            "type": "",
            "metadata": {
                "legend_name": "Mirage",
                "icon": "https://media.contentapi.ea.com/content/dam/apex-legends/images/2019/01/legends-character-tiles/apex-grid-tile-legends-mirage.png.adapt.crop16x9.png",
                "bgimage": "https://trackercdn.com/cdn/apex.tracker.gg/legends/mirage-concept-bg-small.jpg"
            },
            "stats": [
                {
                    "metadata": {
                        "key": "Kills",
                        "name": "Kills",
                        "categoryKey": "combat",
                        "categoryName": "Combat",
                        "isReversed": false
                    },
                    "value": 8,
                    "percentile": 84,
                    "rank": 33915,
                    "displayValue": "8",
                    "displayRank": "33,915"
                },
                {
                    "metadata": {
                        "key": "Damage",
                        "name": "Damage",
                        "categoryKey": "combat",
                        "categoryName": "Combat",
                        "isReversed": false
                    },
                    "value": 3106,
                    "percentile": 27,
                    "rank": 11370,
                    "displayValue": "3,106",
                    "displayRank": "11,370"
                },
                {
                    "metadata": {
                        "key": "Headshots",
                        "name": "Headshots",
                        "categoryKey": "combat",
                        "categoryName": "Combat",
                        "isReversed": false
                    },
                    "value": 13,
                    "percentile": 100,
                    "rank": 5099,
                    "displayValue": "13",
                    "displayRank": "5,099"
                }
            ]
        },
        {
            "id": "legend_3",
            "type": "",
            "metadata": {
                "legend_name": "Caustic",
                "icon": "https://media.contentapi.ea.com/content/dam/apex-legends/images/2019/01/legends-character-tiles/apex-grid-tile-legends-caustic.png.adapt.crop16x9.png",
                "bgimage": "https://trackercdn.com/cdn/apex.tracker.gg/legends/caustic-concept-bg-small.jpg"
            },
            "stats": [
                {
                    "metadata": {
                        "key": "Kills",
                        "name": "Kills",
                        "categoryKey": "combat",
                        "categoryName": "Combat",
                        "isReversed": false
                    },
                    "value": 5,
                    "percentile": 72,
                    "rank": 10472,
                    "displayValue": "5",
                    "displayRank": "10,472"
                },
                {
                    "metadata": {
                        "key": "Damage",
                        "name": "Damage",
                        "categoryKey": "combat",
                        "categoryName": "Combat",
                        "isReversed": false
                    },
                    "value": 2104,
                    "percentile": 13,
                    "rank": 2126,
                    "displayValue": "2,104",
                    "displayRank": "2,126"
                },
                {
                    "metadata": {
                        "key": "Headshots",
                        "name": "Headshots",
                        "categoryKey": "combat",
                        "categoryName": "Combat",
                        "isReversed": false
                    },
                    "value": 10,
                    "percentile": 4,
                    "rank": 854,
                    "displayValue": "10",
                    "displayRank": "854"
                }
            ]
        },
        {
            "id": "legend_7",
            "type": "",
            "metadata": {
                "legend_name": "Lifeline",
                "icon": "https://media.contentapi.ea.com/content/dam/apex-legends/images/2019/01/legends-character-tiles/apex-grid-tile-legends-lifeline.png.adapt.crop16x9.png",
                "bgimage": "https://trackercdn.com/cdn/apex.tracker.gg/legends/lifeline-concept-bg-small.jpg"
            },
            "stats": [
                {
                    "metadata": {
                        "key": "Kills",
                        "name": "Kills",
                        "categoryKey": "combat",
                        "categoryName": "Combat",
                        "isReversed": false
                    },
                    "value": 12,
                    "percentile": 76,
                    "displayValue": "12",
                    "displayRank": ""
                }
            ]
        }
    ],
    "metadata": {
        "statsCategoryOrder": [
            "combat",
            "game",
            "weapons"
        ],
        "platformId": 5,
        "platformUserHandle": "ItsSpress",
        "accountId": "b2553208-d89f-4b5d-b08d-5b61dc8d68fe",
        "cacheExpireDate": "3/2/2019 9:11:33 AM",
        "level": 32
    },
    "stats": [
        {
            "metadata": {
                "key": "Level",
                "name": "Level",
                "categoryKey": "combat",
                "categoryName": "Combat",
                "isReversed": false
            },
            "value": 32,
            "percentile": 57,
            "displayValue": "32",
            "displayRank": ""
        },
        {
            "metadata": {
                "key": "Kills",
                "name": "Kills",
                "categoryKey": "combat",
                "categoryName": "Combat",
                "isReversed": false
            },
            "value": 157,
            "percentile": 50,
            "displayValue": "157",
            "displayRank": ""
        },
        {
            "metadata": {
                "key": "Damage",
                "name": "Damage",
                "categoryKey": "combat",
                "categoryName": "Combat",
                "isReversed": false
            },
            "value": 34055,
            "percentile": 36,
            "displayValue": "34,055",
            "displayRank": ""
        },
        {
            "metadata": {
                "key": "Finishers",
                "name": "Finishers",
                "categoryKey": "game",
                "categoryName": "Game",
                "isReversed": false
            },
            "value": 0,
            "displayValue": "0",
            "displayRank": ""
        },
        {
            "metadata": {
                "key": "ArKills",
                "name": "AR Kills",
                "categoryKey": "weapons",
                "categoryName": "Weapons",
                "isReversed": false
            },
            "value": 0,
            "displayValue": "0",
            "displayRank": ""
        }
    ]
}
}

如果我做这样的事情...

print(json.data.children.map{$0.stats.map{$0.metadata.name}})

我知道...

[["Kills", "Damage", "Headshots"], ["Kills", "Finishers"], ["Kills", "Headshots", "Legend Specific 2"], ["Kills", "Legend Specific 2"], ["Kills", "AR Kills"], ["Kills", "Damage", "Headshots"], ["Kills", "Damage", "Headshots"], ["Kills"]]

1 个答案:

答案 0 :(得分:1)

问题是自白的。

您正在将组织良好的结构拆分为单独的数组。 不要那样做。永远不会

保持结构层次结构并从结构中获取值。

似乎children数组代表数据源数组,因此删除

drop index (select indexname from pg_indexes where tablename in ('t1','t2'/*list of tables of which index I want to drop*/) ;

并声明一个数组

var legendImgArr = [""]
//legend name
var legendTitle = [""]
//data set 1
var title1 = [""]
var data1 = [0]
//data set 2
var title2 = [""]
var data2 = [0]
//data set 3
var title3 = ["nil"]
var data3 = [0]

并在解码JSON后分配var children = [Child]() 数组

[Child]

还删除所有为这些已删除数组分配值的行。


为方便起见,创建一个辅助函数以从self.children = json.data.children 中提取给定索引的数据:

stats

然后在func nameValue(for stats: [Stat], at index : Int) -> (String, String) { guard index < stats.count else { return ("", "") } let stat = stats[index] return (stat.metadata.name, String(stat.value)) } 中将孩子带到索引路径一次

cellforRow

,然后将值分配给标签

let child = children[indexPath.row] 

这比不可控制的可怕单独阵列 更加有效和安全。