JSON转换NSObject工具:MBJSONModel
jopen
11年前
MBJSONModel 是快速,轻量级的 JSON 转换 NSObject 的工具。
@interface User : MBJSONModel @property (nonatomic, copy) NSString *name; @property (nonatomic) NSDate *birthDate; @property (nonatomic) NSArray *tweets; @end @implementation User + (NSDictionary *)JSONKeyTranslationDictionary { return @{ @"user_name" : @"name", @"birth_date" : @{@"isDate" : @YES, @"format" : @"yyyy-MM-dd", @"property" : @"birthDate"}, @"data.user.tweets" : @{@"class" : NSStringFromClass([Tweet class]), @"relationship" : @"tweets", @"isArray" : @YES}, } } @end
然后,如果你有一个JSON字典,看起来像这样:
{ "user_name" : "Jon Snow", "birth_date" : "1970/03/29", "data" : { "user" : { "tweets" : [ { "text" : "hello world" } ] } } }
You can easily do:
User *userObj = [User modelFromJSONDictionary:jsonDict];