通过解析JSON-SwiftyJSON创建一个Class对象数组

时间:2016-12-12 02:47:31

标签: ios arrays json swift3 swifty-json

我有一个带有一些变量的类,我想通过使用SwiftyJSON解析JSON输出来创建这个类的对象数组(我是SwiftyJSON的新手,所以在创建一个时面临一些问题)和使用表格显示它们

有人可以帮助我创建自定义类对象数组。

我的模特课程:

    class RankDetailsModel
    {
        var key:String!
        var currentMonthRank: Int!
        var currentRevenue:Double!
        var lastMonthRank:Int!
        var lastMonthRevenue:Double!

        init?(rank: [String: Any]) {

            guard let key = rank["Key"] as? String,
            let currentMonthRank = rank["CurrentMonthRank"] as? Int,
            let currentRevenue = rank["CurrentRevenue"] as? Int,
            let lastMonthRank = rank["LastMonthRank"] as? String,
            let lastMonthRevenue = rank["LastMonthRevenue"] as? String
                else{
                    return nil
            }

            self.key = key;
        self.currentMonthRank = currentMonthRank;
        self.currentRevenue = Double(currentRevenue);
        self.lastMonthRank = Int(lastMonthRank);
        self.lastMonthRevenue = Double(lastMonthRevenue);
        }
    }

JSON响应:

{ "AfxRank":
    [{
        "CurrentMonthRank" : 1,
        "Key" : "WSM",
        "CurrentRevenue" : 28834854,
        "LastMonthRevenue": null,
        "LastMonthRank": null
      },
      {
        "CurrentMonthRank" : 2,
        "Key" : "SAM",
        "CurrentRevenue" : 21880000,
        "LastMonthRevenue": null,
        "LastMonthRank": null
      },
      {
        "CurrentMonthRank" : 3,
        "Key" : "CI",
        "CurrentRevenue" : 11380000,
        "LastMonthRevenue": null,
        "LastMonthRank": null
      }]
}

我在VC中的代码:

var rankArray : [RankDetailsModel] = []
switch(response.result) {
                case .success(_):
                    if let data = response.result.value{

                        let jsonD = JSON(data) 
                        let dataNew = jsonD.rawString()?.data(using: String.Encoding.utf8) 
                        let jsonFinal = JSON(dataNew)
                        print(jsonFinal["AfxRank"]) //successfully printing the json array as shown above
                        rankArray = jsonFinal["AfxRank"].arrayValue as! [RankDetailsModel] //throwing an error 'RankDetailsModel' is not a subtype of JSON

//can I try something like this. (I didnt use SwifyJson and Alamofire in this case)
//if let userDict = json["AfxRank"] as? [String:Any]
//                        {
//                            guard let userObject = //RankDetailsModel(rank:userDict) else {
//                                print("Failed to create user from //dictionary")
//                                return
//                            }   
                    }
                    break

                case .failure(_):
                    print(response.result.error)
                    break

                }

1 个答案:

答案 0 :(得分:1)

您的密钥AfxRank包含Array而非字典,因此您需要输入强制转换为[[String:Any]],然后遍历循环并append [RankDetailsModel]数组中的对象var rankArray = [RankDetailsModel]() if let userArray = json["AfxRank"].arrayValue as? [[String:Any]] { for dic in userArray { if let rankDetailObj = RankDetailsModel(rank: dic) { rankArray.append(rankDetailObj) } } } 1}}。

vector