Swift之Json->Model &&NetWorking基本使用

说明:Alamofire请求服务器数据 ,HandyJSON转换JSON->Model

  • 使用库:
pod 'Alamofire', '~> 4.0.1' #用于请求服务器数据
pod 'HandyJSON', '~> 1.1.0' #用于Josn->Model

//特别说明:swift3.0版本cocopods  Podfile文件结构
platform:ios,'9.0'

use_frameworks!

target 'SwiftSaleEstate' do

pod 'Alamofire', '~> 4.0.1'
pod 'HandyJSON', '~> 1.1.0'

post_install do |installer|
    installer.pods_project.targets.each do |target|
        target.build_configurations.each do |config|
            config.build_settings['SWIFT_VERSION'] = '3.0'  ## or '3.0'
        end
    end
end
end

1.基本使用

//基本JSON -> Model
let jsonString = "{\"ResultNo\":0,\"Result\":{\"DealAvgPrice\":24520.61,\"RelativeRatio\":0,\"RelativeRatioPercent\":10.8368835360245,\"SaleNumber\":18101,\"RentNumber\":9611,\"GScopeId\":22,\"GScopeName\":\"天津\",\"GScopeLevel\":1,\"DealNumber\":1380,\"RentDealNumber\":1713,\"SaleAvgPrice\":25077.7149111487,\"DataMonth\":9},\"Total\":0}"
if let jsonEntity = JSONDeserializer.deserializeFrom(json: jsonString) {
    print(jsonEntity.Total)
    print(jsonEntity.ResultNo)
    print(jsonEntity.Result?.GScopeName)    
}

//Alamofire请求及JSON->Model
let url = GlobalRequestUrl.requestGetGScopeLastMonthDealInfoUrl
Alamofire.request(url, method: .get, parameters: ["GScopeId":"011","GScopeLevel":"1"]).responseString { response in
    if let JSON = response.result.value {
        if let jsonEntity = JSONDeserializer.deserializeFrom(json: JSON) {
            print(jsonEntity.Total)
            print(jsonEntity.ResultNo)
            print(jsonEntity.Result?.GScopeName)
        }
    }
}
  1. EstateAreaRegionResponseEntity结构体
import HandyJSON

struct EstateAreaRegionEntity: HandyJSON {
    var DataMonth:Int?
    var DealAvgPrice:Double?
    var DealNumber:Int?
    var GScopeId:Int?
    var GScopeLevel:Int?
    var GScopeName:String?
    var RelativeRatio:Double?
    var RelativeRatioPercent:Double?
    var RentDealNumber:Int?
    var RentNumber:Int?
    var SaleAvgPrice:Double?
    var SaleNumber:Int?
}

struct EstateAreaRegionResponseEntity: HandyJSON {
    var ResultNo:Int?
    var Total:Int?
    var Result:EstateAreaRegionEntity?
}

2.自定义解析方式

  • HandyJSON还提供了一个扩展功能,允许自行定义Model类某个字段的解析Key、解析方式。
    如:某个Model中,我们不想使用和服务端约定的key作为属性名,可以自定义一个
import HandyJSON

struct EstateAreaRegionEntity: HandyJSON {
    var DataMonth:Int?
    var DealAvgPrice:Double?
    var DealNumber:Int?
    var GScopeId:Int?
    var GScopeLevel:Int?
    var GScopeName:String?
    var RelativeRatio:Double?
    var RelativeRatioPercent:Double?
    var RentDealNumber:Int?
    var RentNumber:Int?
    var SaleAvgPrice:Double?
    var SaleNumber:Int?
}

struct EstateAreaRegionResponseEntity: HandyJSON {
    var resultNo:Int?
    var total:Int?
    var result:EstateAreaRegionEntity?
    
    //自定义解析方式
    mutating func mapping(mapper: HelpingMapper) {
        mapper.specify(property: &resultNo, name: "ResultNo")
        mapper.specify(property: &total, name: "Total")
        mapper.specify(property: &result, name: "Result")
    }
}

你可能感兴趣的:(Swift之Json->Model &&NetWorking基本使用)