美文网首页
iOS开发中JSONKit

iOS开发中JSONKit

作者: geekAppke | 来源:发表于2018-09-11 14:14 被阅读158次

OC的JSON操作方法

#import <Foundation/Foundation.h>

@interface NSString(JSONCategories)

+(NSString *)stringFromJSONData:(NSData *)jsonData;
+(NSString *)stringFromJSONData:(NSData *)jsonData options:(NSJSONReadingOptions)opt error:(NSError **)error;

-(NSData *)toJSON;
-(NSData *)toJSONWithoptions:(NSJSONWritingOptions)opt error:(NSError **)error;

-(NSArray *)arrayFromJSONString;
-(NSDictionary *)dictionaryFromJSONString;

@end

@interface NSArray(JSONCategories)

+(NSArray *)arrayFromJSONData:(NSData *)jsonData;
+(NSArray *)arrayFromJSONData:(NSData *)jsonData options:(NSJSONReadingOptions)opt error:(NSError **)error;

-(NSData *)toJSON;
-(NSData *)toJSONWithoptions:(NSJSONWritingOptions)opt error:(NSError **)error;

-(NSString *)JSONString;

@end

@interface NSDictionary(JSONCategories)

+(NSDictionary *)dictionaryFromJSONData:(NSData *)jsonData;
+(NSDictionary *)dictionaryFromJSONData:(NSData *)jsonData options:(NSJSONReadingOptions)opt error:(NSError **)error;

-(NSData *)toJSON;
-(NSData *)toJSONWithoptions:(NSJSONWritingOptions)opt error:(NSError **)error;

-(NSString *)JSONString;

@end

#import "LOJSONKit.h"

@implementation NSString(JSONCategories)

+(NSString *)stringFromJSONData:(NSData *)jsonData
{
    
    __autoreleasing NSError* error = nil;
    id result = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error];
    if (error != nil) return nil;
    return result;
    
}

+(NSString *)stringFromJSONData:(NSData *)jsonData options:(NSJSONReadingOptions)opt error:(NSError **)error
{
    id result = [NSJSONSerialization JSONObjectWithData:jsonData options:opt error:error];
    if (error != nil) return nil;
    return result;
}

-(NSData *)toJSON
{
    if ([NSJSONSerialization isValidJSONObject:self]) {
        NSError* error = nil;
        id result = [NSJSONSerialization dataWithJSONObject:self options:kNilOptions error:&error];
        if (error != nil) return nil;
        return result;
    }else{
        return [self dataUsingEncoding:NSUTF8StringEncoding];
    }
}

-(NSData *)toJSONWithoptions:(NSJSONWritingOptions)opt error:(NSError **)error
{
    if ([NSJSONSerialization isValidJSONObject:self]) {
        id result = [NSJSONSerialization dataWithJSONObject:self options:opt error:error];
        if (error != nil) return nil;
        return result;
    }else{
        return nil;
    }
}

-(NSArray *)arrayFromJSONString
{
    NSData* data = [self dataUsingEncoding:NSUTF8StringEncoding];
    
    return  [NSArray arrayFromJSONData:data];
}

-(NSDictionary *)dictionaryFromJSONString
{
    NSData* data = [self dataUsingEncoding:NSUTF8StringEncoding];
    
    return  [NSDictionary dictionaryFromJSONData:data];
}

@end

@implementation NSArray(JSONCategories)

+(NSArray *)arrayFromJSONData:(NSData *)jsonData
{
    __autoreleasing NSError* error = nil;
    id result = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error];
    if (error != nil) return nil;
    return result;
}
+(NSArray *)arrayFromJSONData:(NSData *)jsonData options:(NSJSONReadingOptions)opt error:(NSError **)error
{
    id result = [NSJSONSerialization JSONObjectWithData:jsonData options:opt error:error];
    if (error != nil) return nil;
    return result;
}

-(NSData *)toJSON
{
    if ([NSJSONSerialization isValidJSONObject:self]) {
        NSError* error = nil;
        id result = [NSJSONSerialization dataWithJSONObject:self options:kNilOptions error:&error];
        if (error != nil) return nil;
        return result;
    }else{
        return nil;//-.[NSKeyedArchiver archivedDataWithRootObject:self];
    }
}

-(NSData *)toJSONWithoptions:(NSJSONWritingOptions)opt error:(NSError **)error
{
    if ([NSJSONSerialization isValidJSONObject:self]) {
        id result = [NSJSONSerialization dataWithJSONObject:self options:opt error:error];
        if (error != nil) return nil;
        return result;
    }else{
        return nil;
    }
}

-(NSString *)JSONString
{
    NSData* data = [self toJSON];
    return [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
}

@end

@implementation NSDictionary(JSONCategories)

+(NSDictionary *)dictionaryFromJSONData:(NSData *)jsonData
{
    if (jsonData==nil) {
        return nil;
    }
    __autoreleasing NSError* error = nil;
    id result = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error];
    if (error != nil) return nil;
    return result;
}

+(NSDictionary *)dictionaryFromJSONData:(NSData *)jsonData options:(NSJSONReadingOptions)opt error:(NSError **)error
{
    id result = [NSJSONSerialization JSONObjectWithData:jsonData options:opt error:error];
    if (error != nil) return nil;
    return result;
}

-(NSData *)toJSON
{
    if ([NSJSONSerialization isValidJSONObject:self]) {
        NSError* error = nil;
        id result = [NSJSONSerialization dataWithJSONObject:self options:kNilOptions error:&error];
        if (error != nil) return nil;
        return result;
    }else{
        return nil;//[NSKeyedArchiver archivedDataWithRootObject:self];
    }
}

-(NSData *)toJSONWithoptions:(NSJSONWritingOptions)opt error:(NSError **)error
{
    if ([NSJSONSerialization isValidJSONObject:self]) {
        id result = [NSJSONSerialization dataWithJSONObject:self options:opt error:error];
        if (error != nil) return nil;
        return result;
    }else{
        return nil;
    }
}

-(NSString *)JSONString
{
    NSData* data = [self toJSON];
    return [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
}
@end

相关文章

网友评论

      本文标题:iOS开发中JSONKit

      本文链接:https://www.haomeiwen.com/subject/yekugftx.html