SwiftyJSON中JSONArray转ModelArray

model模型

struct QuationCoinExchangeInfoListModel {
    
    var alias:String
    var anchor:String
    var market_name:String
    var percent_change_utc0:String
//    var percent_change_utc0_0:String // 不需要
    var price:String
    var price_cny:String
    var price_usd:String
    var symbol:String
    var volume_24h:String
    var volume_24h_from:String
    var volume_24h_usd:String
    
    init(_ json:JSON) {
        
        alias = json["alias"].stringValue
        anchor = json["anchor"].stringValue
        market_name = json["market_name"].stringValue
        percent_change_utc0 = json["percent_change_utc0"].stringValue
        price = json["price"].stringValue
        price_cny = json["price_cny"].stringValue
        price_usd = json["price_usd"].stringValue
        symbol = json["symbol"].stringValue
        volume_24h = json["volume_24h"].stringValue
        volume_24h_from = json["volume_24h_from"].stringValue
        volume_24h_usd = json["volume_24h_usd"].stringValue
        
    }
    
    static func modelArrayWithJsonArray(jsonArray:[JSON]) -> [QuationCoinExchangeInfoListModel] {
        
        var modelArray = [QuationCoinExchangeInfoListModel]()
        for json in jsonArray {
            modelArray.append(QuationCoinExchangeInfoListModel(json))
        }
        
        return modelArray
    }
    
}

// 包容model的模型


struct QuationCoinExchangeInfoDataModel {
    
    var market_list:[QuationCoinExchangeInfoListModel]
    
    init(_ json: JSON) {
        
        1、原始的手动解析
        market_list = QuationCoinExchangeInfoListModel.modelArrayWithJsonArray(jsonArray: json["market_list"].arrayValue)
        2、简化
        market_list = json["market_list"].arrayValue.map({ (json) ->QuationCoinExchangeInfoListModel in
            
            return QuationCoinExchangeInfoListModel(json)
        })
        3、最简化,推荐使用
        /// !!!!!
        market_list = json["market_list"].arrayValue.map { QuationCoinExchangeInfoListModel($0) }
        
    }
}

你可能感兴趣的:(SwiftyJSON中JSONArray转ModelArray)