简体   繁体   English

从JSON /字典创建模型对象,而无需使用Objective-c运行时和硬编码或键

[英]Create a Model Object from JSON/Dictionary without Objective-c Runtime & hard coding or keys

I am trying to parse a JSON and create a Model(a nested class structure). 我正在尝试解析JSON并创建一个Model(嵌套的类结构)。 I can do it using JSONModel (available on Github) but I do not want to use Obj-c Runtime. 我可以使用JSONModel(在Github上可用)来实现,但是我不想使用Obj-c Runtime。

Is there any other way to achieve this? 还有其他方法可以做到这一点吗? Lets see what I did with some dummy code. 让我们看看我对一些伪代码所做的事情。

NSDictionary *dictionary = @{@"personalDetail":@{@"name":@"Anoop kumar vaidya",
                                                   @"dob":@"3 Aug 1981",
                                                   @"sex":@"Male"},
                             @"workDetail":@{@"current":@"Photon",
                                               @"y2014":@"TechM",
                                               @"y2012":@"Exilant",
                                               @"y2010":@"RSG"},
                             @"educationDetail":@{@"pg":@"Mca - 63%",
                                                    @"ug":@"Bca -63%",
                                                    @"s12":@"58%",
                                                    @"s10":@"66%"
                                                    }
                             };


self.model = [[MainModel alloc] initWithMainModel:dictionary];

Implementation of MainModel , here you see the keys are hard coded. MainModel实现,在这里您看到按键是硬编码的。 I want to avoid it, to make it generic. 我想避免它,使其通用。

-(id)initWithMainModel:(NSDictionary *)dict{

    for (NSString *key in dict) {
        [self putValue:dict[key] forKey:key];
    }
    return self;
}


-(void)putValue:(NSDictionary *)value forKey:(NSString *)key{

    if ([key isEqualToString:@"personalDetail"]) { 
        self.personalDetail = [[Personal alloc] initWithDictionary:value];
    }
    else if ([key isEqualToString:@"workDetail"]){
        self.workDetail = [[Work alloc] initWithDictionary:value];
    }
    else if ([key isEqualToString:@"educationDetail"]){
        self.educationDetail = [[Education alloc] initWithDictionary:value];
    }
}

Implementation of Personal . Personal实施。 Similarly other two classes, Work & Education are implemented. 类似地,其他两个课程,即“ WorkEducation也被实施。

-(id)initWithDictionary:(NSDictionary *)dict{

    for (NSString *key in dict) {
        [self putValue:dict[key] forKey:key];
    }
    return self;
}


-(void)putValue:(NSString *)value forKey:(NSString *)key{
    [self setValue:value forKey:key];
}

You can use the key and extract the class name of it. 您可以使用该键并提取它的类名。 Then you get the class for this identifier witch NSClassFromString() to get the class and instantiate it with +alloc as usual: 然后,获取该标识符女巫NSClassFromString()的类,以获取该类并像往常一样使用+alloc实例化它:

key = …;
if ([key hasSuffix:@"Detail"]) 
{
  NSString *className = [key substringToIndex:[key length]-[@"Detail length]];
  className = [[[className substringToIndex:1] uppercaseString] stringByAppendingString:[className substringFromIndex:1]];
}

id instance = [[NSClassFromString( className ) alloc] initWithDictionary:…];

I said: You can do it this way. 我说:您可以这样做。 I did not say: You should . 我没有说:你应该

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM