Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Parsing JSON objects using Mantle framework in Objective C

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.4k
    Comment on it

    Hi Readers,

    Mantle is latest and reliable framework which we can use while parsing JSON objects coming from server end. We can do this by simply initialize a class of type MTLModel.

    We can understand framework working with following examples:

    {
        "string": "Click Here",
        "style": "bold",
        "name": "text1",
        "alignment": "center",
    }


    Now create a objective-c class and in .h file implement following code:

    @interface TextDataModel : MTLModel<MTLJSONSerializing>
    
    @property (nonatomic, copy, readwrite) NSString *string;
    @property (nonatomic, copy, readwrite) NSString *style;
    @property (nonatomic, copy, readwrite) NSString *name;
    @property (nonatomic, copy, readwrite) NSString *alignment;
    @end

    and in .m file add following:

    @implementation TextDataModel
    
    + (NSDictionary *)JSONKeyPathsByPropertyKey
    {
        // model_property_name : json_field_name
        return @{
                 @"string":@"string",
                 @"style":@"style",
                 @"name":@"name",
                 @"alignment":@"alignment",
                 };
    }
    @end

    This way you can simply create mantle objects and can assign these property from data coming from server.

    Now we can move to more complex JSON like:

    {
        widget": {
                "debug": "on",
                "window": {
                                    "title": "Sample Konfabulator Widget",
                                    "name": "main_window",
                                }
    
                       }
    }

    For that you have to include code in .h as following:

    @interface WidgetModel : MTLModel<MTLJSONSerializing>
    
    @property (nonatomic, copy, readwrite) NSString *debug;
    @property (nonatomic, copy, readwrite) NSString *title;
    @property (nonatomic, copy, readwrite) NSString *name;
    @end

    and in .m file add following:

    @implementation WidgetModel
    
    + (NSDictionary *)JSONKeyPathsByPropertyKey
    {
        // model_property_name : json_field_name
        return @{
                 @"debug":@"debug",
                 @"title":@"window.title",
                 @"name":@"window.name",
                 };
    }
    @end
    
    

    You can notice a change here as window.title and window.name which we parse into json object with key name "window". 

    Now server end response dictionary can be used to get required objects as :

    NSError *error = nil;
    id model = [MTLJSONAdapter modelOfClass:NSClassFromString(className) fromJSONDictionary:JSON error:&error];

    Here className is class of model as we previously used WidgetModel and JSON is dictionary coming from server.

    HAPPY CODING :)

     

 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: