Objective-c 自定對象 轉 JSON

#pragma mark - NSObject to JSON -


static NSDateFormatter *reverseFormatter;


- (NSDateFormatter *)getReverseDateFormatter {

    if (!reverseFormatter) {

        NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];

        reverseFormatter = [[NSDateFormatter alloc] init];

        [reverseFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];

        [reverseFormatter setLocale:locale];

    }

    return reverseFormatter;

}


- (NSDictionary *)dictionaryWithPropertiesOfObject:(id)obj {

    NSMutableDictionary *dict = [NSMutableDictionary dictionary];

    

    unsigned count;

    objc_property_t *properties = class_copyPropertyList([obj class], &count);

    

    for (int i = 0; i < count; i++) {

        NSString *key = [NSString stringWithUTF8String:property_getName(properties[i])];

        id object = [obj valueForKey:key];

        

        if (object) {

            if ([object isKindOfClass:[NSArray class]]) {

                NSMutableArray *subObj = [NSMutableArray array];

                for (id o in object) {

                    [subObj addObject:[self dictionaryWithPropertiesOfObject:o]];

                }

                dict[key] = subObj;

            }

            else if ([object isKindOfClass:[NSString class]]) {

                dict[key] = object;

            } else if ([object isKindOfClass:[NSDate class]]) {

                dict[key] = [[self getReverseDateFormatter] stringFromDate:(NSDate *) object];

            } else if ([object isKindOfClass:[NSNumber class]]) {

                dict[key] = object;

            } else if ([[object class] isSubclassOfClass:[NSObject class]]) {

                dict[key] = [self dictionaryWithPropertiesOfObject:object];

            }

        }

        

    }

    return dict;

}

發佈了149 篇原創文章 · 獲贊 38 · 訪問量 71萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章