Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to parse JSON in Swift with ObjectMapper

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 2.06k
    Comment on it

    Hi Readers,

    Every app needs to parse JSON data coming from server and ObjectMapper is a powerful tool to achieve that. It can be easily done with provided pod which can be install by adding following line into podfile: 

    pod 'ObjectMapper'

    Now after installing ObjectMapper into your project you need to create objects according to data coming into the JSON response like:

    import Foundation
    import ObjectMapper
    
    class EventModel: Mappable {
        /* Attributes */
        
        internal var eventId : Int?
        internal var eventName = ""
        
        /* Constructors */
        internal init?() {
            // Empty Constructor
        }
        
        required internal init?(_ map: Map) {
            mapping(map)
        }
        
        /* Methods */
        internal func mapping(map: Map) {
            eventId <- map["id"]
            eventName <- map["eventName"]
        }
    }
    
    
    
    class EventResponseModel: Mappable {
        /* Attributes */
        
        internal var totalEvents : Int?
        internal var allEventsArray = [EventModel]()
        internal var pageNumber : Int?
        /* Constructors */
        
        internal init?() {
            // Empty Constructor
        }
        
        required internal init?(_ map: Map) {
            mapping(map)
        }
        
        /* Methods */
        internal func mapping(map: Map) {
            totalEvents <- map["totalEvents"]
            allEventsArray <- map["allEvents"]
            pageNumber <- map["pageNumber"]
    
        }
    }

    Above code is only essential part of mapping through ObjectMapper. You can see that we have a JSON here as EventResponseModel in which we are getting total event as number, page number as number and all events array which have event model type JSON object. Event model have attributes as event id and event name. By overriding mapping function of Mappable, we can parse JSON data into our object attribute.

    Now we are done with object class. We need to create EventResponseObject when we get data from server which will be reused later. Object can be parsed like following code snippet:

    var eventResponseModel:EventResponseModel = Mapper<EventResponseModel>().map(responseDictionary)!

    Here we can get eventResponseModel by which we retrieve data further. Hope it helps.

    Thanks for reading.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: