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

 

转载于:https://www.cnblogs.com/weierpeng/p/4665865.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值