JSONString 与 JSONData 与字典或者数组互相转化

JSONString 与 JSONData 与字典或者数组互相转化

JSON

JSON相关的,数据彼此间的转化进行了简单地封装,源码如下,支持arc与非arc

YXJSON.h + YXJSON.m

//

//  YXJSON.h

//

//  JSONString 与 JSONData 与 字典或者数组互相转化

//

//  Copyright (c) 2014年 YouXian. All rights reserved.

//



#import <Foundation/Foundation.h>



@interface YXJSON : NSObject



/**

 将字典或者数组转换为JSON格式字符串

 

 @return JSON格式字符串

 */

+ (NSString *)JSONStringWithDictionaryOrArray:(id)dictionaryOrArray;



/**

 将字典或者数组转换为JSON的Data

 

 @return JSON的Data

 */

+ (NSData *)JSONSDataWithDictionaryOrArray:(id)dictionaryOrArray;



/**

 将JSON格式字符串转换为字典或者数组

 

 @return 字典或者数组

 */

+ (id)dictionaryOrArrayWithJSONSString:(NSString *)jsonString;



/**

 将JSON的Data转换为字典或者数组

 

 @return 字典或者数组

 */

+ (id)dictionaryOrArrayWithJSONSData:(NSData *)jsonData;



@end
//

//  YXJSON.m

//

//  JSONString 与 JSONData 与 字典或者数组互相转化

//

//  Copyright (c) 2014年 YouXian. All rights reserved.

//



#if __has_feature(objc_arc)

#define YX_release(obj)

#define YX_autorelease(obj)

#else

#define YX_release(obj)      [obj release]

#define YX_autorelease(obj)  [obj autorelease]

#endif



#import "YXJSON.h"



@implementation YXJSON



+ (NSString *)JSONStringWithDictionaryOrArray:(id)dictionaryOrArray

{

    if (dictionaryOrArray == nil)

    {

        return nil;

    }

    

    //options: Pass 0 if you don't care about the readability of the generated string

    NSData *data = [NSJSONSerialization dataWithJSONObject:dictionaryOrArray

                                                   options:NSJSONWritingPrettyPrinted

                                                     error:nil];

    

    if (data == nil)

    {

        return nil;

    }

    

    NSString *string = [[NSString alloc] initWithData:data

                                             encoding:NSUTF8StringEncoding];

    YX_autorelease(string);

    

    return string;

}



+ (NSData *)JSONSDataWithDictionaryOrArray:(id)dictionaryOrArray

{

    if (dictionaryOrArray == nil)

    {

        return nil;

    }

    

    //options: Pass 0 if you don't care about the readability of the generated string

    NSData *data = [NSJSONSerialization dataWithJSONObject:dictionaryOrArray

                                                   options:NSJSONWritingPrettyPrinted

                                                     error:nil];

    return data;

}



+ (id)dictionaryOrArrayWithJSONSString:(NSString *)jsonString

{

    if (jsonString == nil)

    {

        return nil;

    }

    

    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];

    

    return [NSJSONSerialization JSONObjectWithData:jsonData

                                           options:NSJSONReadingMutableLeaves|| NSJSONReadingMutableContainers

                                             error:nil];

}



+ (id)dictionaryOrArrayWithJSONSData:(NSData *)jsonData

{

    if (jsonData == nil)

    {

        return nil;

    }

    

    return [NSJSONSerialization JSONObjectWithData:jsonData

                                           options:NSJSONReadingMutableLeaves|| NSJSONReadingMutableContainers

                                             error:nil];

}



@end

没有将其单独的封装到 NSString NSData NSArray NSDictionary 相关类目中,实际上用类目的方式使用更加方便,有需求可以自己弄,但文件多较为繁琐,本人不习惯.

 

 

 

你可能感兴趣的:(String)