JSON_HELPER
目前功能
将NSObject子类转成JSON格式
对象模型 《-》 属性列表 《-》 JSON格式
JSON和对象模型之间来回转换,
转到属性列表后用NSJSONSerialization和JSON之间转换。
先写出来在看看改改BUG。
// // JSON HELPER.h // JSON helper // // Created by 彭威 on 15/7/21. // Copyright (c) 2015年 biophy.nju.edu.cn. All rights reserved. // #import <Foundation/Foundation.h> #import <objc/runtime.h> @interface JSON_HELPER : NSObject @property (nonatomic,strong) NSDateFormatter *dateFormatter; @property (nonatomic,strong) NSDictionary *objDic; - (NSData *)dataFromObject:(id) obj; - (id)objectFromJSONData:(NSData *)jsonData; @end // // JSON HELPER.m // JSON helper // // Created by 彭威 on 15/7/21. // Copyright (c) 2015年 biophy.nju.edu.cn. All rights reserved. // #import "JSON_HELPER.h" @implementation JSON_HELPER - (id)objectFromJSONData:(NSData *)jsonData; { if (!jsonData) { return nil; } id responseData = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil]; if (![self.objDic valueForKey:@"Top"]){ return responseData; } return [self NSObjectFromJSONObject:responseData keyName:self.objDic[@"Top"]]; } - (id)NSObjectFromJSONObject:(id) json keyName:(NSString *)key{ if (!json) { return nil; } if ([json isKindOfClass:[NSString class]]) { return json; }else if ([json isKindOfClass:[NSArray class]]) { NSMutableArray *objArr; for (id obj in (NSArray *)json) { [objArr addObject:[self NSObjectFromJSONObject:obj keyName:key]]; } return objArr; } else if ([json isKindOfClass:[NSDictionary class]]){ if ([self.objDic[key] isEqual:[NSNull null]]) { NSMutableDictionary *objDic = [[NSMutableDictionary alloc] init]; NSArray *keys = [(NSDictionary *)json allKeys]; for (NSString *subObjKey in keys) { id value = [self NSObjectFromJSONObject:json[subObjKey] keyName:subObjKey]; [objDic setValue:value forKey:subObjKey]; } return objDic; }else{ NSString *className = self.objDic[key]; Class topClass = NSClassFromString(className); id jsonClass = [[topClass alloc]init]; u_int count; objc_property_t* properties = class_copyPropertyList(topClass, &count); for (int i = 0; i < count ; i++) { const char* propertyName = property_getName(properties[i]); NSString *propName = [NSString stringWithCString:propertyName encoding:NSUTF8StringEncoding]; [jsonClass setValue:[self NSObjectFromJSONObject:json[propName] keyName:propName] forKey:propName]; } free(properties); return jsonClass; } } return nil; } - (NSData *)dataFromObject:(id)obj{ return [NSJSONSerialization dataWithJSONObject:[self dicFromObject:obj] options:NSJSONWritingPrettyPrinted error:nil]; } - (id) dicFromObject:(id) obj { if (!obj) { return nil; } if([obj isKindOfClass:[NSString class]]){ return obj; }else if ([obj isKindOfClass:[NSDate class]]){ return [self.dateFormatter stringFromDate:obj]; }else if ([obj isKindOfClass:[NSNumber class]]){ return [NSString stringWithFormat:@"%@",obj]; }else if ([obj isKindOfClass:[NSArray class]]) { NSMutableArray *jsonArr = [[NSMutableArray alloc] init]; for (id subObj in (NSArray *)obj) { [jsonArr addObject:[self dicFromObject:subObj]]; } return jsonArr; } else if ([obj isKindOfClass:[NSDictionary class]]){ NSMutableDictionary *jsonDic = [[NSMutableDictionary alloc] init]; NSArray *key = [(NSDictionary *)obj allKeys]; for (NSString *subObjKey in key) { id value = [self dicFromObject:obj[subObjKey]]; [jsonDic setValue:value forKey:subObjKey]; } return jsonDic; }else{ NSMutableDictionary *jsonDic = [[NSMutableDictionary alloc] init]; u_int count; objc_property_t* properties = class_copyPropertyList([obj class], &count); for (int i = 0; i < count ; i++) { const char* propertyName = property_getName(properties[i]); NSString *propName = [NSString stringWithCString:propertyName encoding:NSUTF8StringEncoding]; id propValue = [obj valueForKey:propName]; id propJsonValue = [self dicFromObject:propValue]; [jsonDic setValue:propJsonValue forKey:propName]; } free(properties); return jsonDic; } } - (NSDateFormatter *)dateFormatter{ if (!_dateFormatter) { _dateFormatter = [[NSDateFormatter alloc]init]; [_dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; } return _dateFormatter; } @end