Simple and clean way to convert JSON string to Object in Swift

前端 未结 16 1209
挽巷
挽巷 2020-11-28 23:27

I have been searching for days to convert a fairly simple JSON string to an object type in Swift but with no avail.

Here is the code for web service call:



        
16条回答
  •  Happy的楠姐
    2020-11-29 00:18

    Use swiftyJson swiftyJson

    platform :ios, '8.0'
    use_frameworks!
    
    target 'MyApp' do
    pod 'SwiftyJSON', '~> 4.0'
    end
    

    Usage

    import SwiftyJSON
    
    let json = JSON(jsonObject)
    
    let id = json["Id"].intValue
    let name = json["Name"].stringValue
    let lat = json["Latitude"].stringValue
    let long = json["Longitude"].stringValue
    let address = json["Address"].stringValue
                
    print(id)
    print(name)
    print(lat)
    print(long)
    print(address)
    

提交回复
热议问题