.h文件
// // NSJSONSerialization+Manage.h // SVPullToRefreshDemo // // Created by Fuer on 14-7-4. // copyright (c) 2014年 Home. All rights reserved. // #import <Foundation/Foundation.h> /** * The domain for NSErrors generated by the NSJSONSerialization+UAAdditions methods. */ extern Nsstring * const UAJSONSerializationErrorDomain; NS_ENUM(NSInteger,UAJSONSerializationErrorCode) { UAJSONSerializationErrorCodeInvalidobject }; @interface NSJSONSerialization (Manage) /** * Converts a Foundation object to a JSON formatted Nsstring * @param jsonObject Foundation object to convert * @return Nsstring formatted as JSON,or nil if an error occurs * @note Writing JSON strings with this method defaults to no NSJSONWritingOptions,and does not accept fragments. */ + (Nsstring *)stringWithObject:(id)jsonObject; /** * Converts a Foundation object to a JSON formatted Nsstring * @param jsonObject Foundation object to convert * @param error An NSError pointer for storing errors,if applicable. * @return Nsstring formatted as JSON,and does not accept fragments. */ + (Nsstring *)stringWithObject:(id)jsonObject error:(NSError **)error; /** * Converts a Foundation object to a JSON formatted Nsstring * @param jsonObject Foundation object to convert * @param acceptingFragments `YES` if objects representing JSON value fragments are acceptable,`NO` otherwise. * @return Nsstring formatted as JSON,or nil if an error occurs. * @note Writing JSON strings with this method defaults to no NSJSONWritingOptions. */ + (Nsstring *)stringWithObject:(id)jsonObject acceptingFragments:(BOOL)acceptingFragments; /** * Converts a Foundation object to a JSON formatted Nsstring * @param jsonObject Foundation object to convert * @param acceptingFragments `YES` if objects representing JSON value fragments are acceptable,`NO` otherwise. * @param error An NSError pointer for storing errors,or nil if an error occurs. * @note Writing JSON strings with this method defaults to no NSJSONWritingOptions. */ + (Nsstring *)stringWithObject:(id)jsonObject acceptingFragments:(BOOL)acceptingFragments error:(NSError **)error; /** * Converts a Foundation object to a JSON formatted Nsstring * @param jsonObject Foundation object to convert * @param opt NSJSONWritingOptions options * @return Nsstring formatted as JSON,or nil if an error occurs */ + (Nsstring *)stringWithObject:(id)jsonObject options:(NSJSONWritingOptions)opt; /** * Converts a Foundation object to a JSON formatted Nsstring * @param jsonObject Foundation object to convert * @param opt NSJSONWritingOptions options * @param error An NSError pointer for storing errors,or nil if an error occurs */ + (Nsstring *)stringWithObject:(id)jsonObject options:(NSJSONWritingOptions)opt error:(NSError **)error; /** * Create a Foundation object from JSON string * @param jsonString the JSON Nsstring to convert * @return A Foundation object,or nil if an error occurs. * @note Creating objects with this method defaults to NSJSONReadingMutableContainers options. */ + (id)objectWithString:(Nsstring *)jsonString; /** * Create a Foundation object from JSON string * @param jsonString the JSON Nsstring to convert * @param opt NSJSONReadingOptions * @return A Foundation object,or nil if an error occurs. */ + (id)objectWithString:(Nsstring *)jsonString options:(NSJSONReadingOptions)opt; /** * Create a Foundation object from JSON string * @param jsonString the JSON Nsstring to convert * @param opt NSJSONReadingOptions * @param error An NSError pointer for storing errors,if applicable. * @return A Foundation object,or nil if an error occurs. */ + (id)objectWithString:(Nsstring *)jsonString options:(NSJSONReadingOptions)opt error:(NSError **)error; @end
.m文件
// // NSJSONSerialization+Manage.m // SVPullToRefreshDemo // // Created by Fuer on 14-7-4. // copyright (c) 2014年 Home. All rights reserved. // #import "NSJSONSerialization+Manage.h" @implementation NSJSONSerialization (Manage) Nsstring * const UAJSONSerializationErrorDomain = @"com.urbanairship.json_serialization"; + (Nsstring *)stringWithObject:(id)jsonObject { return [NSJSONSerialization stringWithObject:jsonObject options:0 acceptingFragments:NO error:nil]; } + (Nsstring *)stringWithObject:(id)jsonObject error:(NSError **)error { return [NSJSONSerialization stringWithObject:jsonObject options:0 acceptingFragments:NO error:error]; } + (Nsstring *)stringWithObject:(id)jsonObject options:(NSJSONWritingOptions)opt { return [NSJSONSerialization stringWithObject:jsonObject options:opt acceptingFragments:NO error:nil]; } + (Nsstring *)stringWithObject:(id)jsonObject options:(NSJSONWritingOptions)opt error:(NSError **)error { return [NSJSONSerialization stringWithObject:jsonObject options:opt acceptingFragments:NO error:error]; } + (Nsstring *)stringWithObject:(id)jsonObject acceptingFragments:(BOOL)acceptingFragments { return [NSJSONSerialization stringWithObject:jsonObject options:0 acceptingFragments:acceptingFragments error:nil]; } + (Nsstring *)stringWithObject:(id)jsonObject acceptingFragments:(BOOL)acceptingFragments error:(NSError **)error { return [NSJSONSerialization stringWithObject:jsonObject options:0 acceptingFragments:acceptingFragments error:error]; } + (Nsstring *)stringWithObject:(id)jsonObject options:(NSJSONWritingOptions)opt acceptingFragments:(BOOL)acceptingFragments error:(NSError **)error { if (!jsonObject) { return nil; } if (!acceptingFragments || ([jsonObject isKindOfClass:[NSArray class]] || [jsonObject isKindOfClass:[NSDictionary class]])) { if (![NSJSONSerialization isValidJSONObject:jsonObject]) { if (error) { Nsstring *msg = [Nsstring stringWithFormat:@"Attempted to serialize invalid object: %@",jsonObject]; NSDictionary *info = @{NSLocalizedDescriptionKey:msg}; *error = [NSError errorWithDomain:UAJSONSerializationErrorDomain code:UAJSONSerializationErrorCodeInvalidobject userInfo:info]; } return nil; } NSData *data = [NSJSONSerialization dataWithJSONObject:jsonObject options:opt error:error]; return [[Nsstring alloc] initWithData:data encoding:NSUTF8StringEncoding]; } else { //this is a dirty hack but it works well. while NSJSONSerialization doesn't allow writing of //fragments,if we serialize the value in an array without pretty printing,and remove the //surrounding bracket characters,we get the equivalent result. Nsstring *arrayString = [self stringWithObject:@[jsonObject] options:0 acceptingFragments:NO error:error]; return [arrayString substringWithRange:NSMakeRange(1,arrayString.length-2)]; } } + (id)objectWithString:(Nsstring *)jsonString { return [self objectWithString:jsonString options:NSJSONReadingMutableContainers]; } + (id)objectWithString:(Nsstring *)jsonString options:(NSJSONReadingOptions)opt { return [self objectWithString:jsonString options:opt error:nil]; } + (id)objectWithString:(Nsstring *)jsonString options:(NSJSONReadingOptions)opt error:(NSError **)error { if (!jsonString) { return nil; } return [NSJSONSerialization JSONObjectWithData: [jsonString dataUsingEncoding:NSUTF8StringEncoding] options: opt error: error]; } @end
option参数说明. enum { NSJSONReadingMutableContainers = (1UL << 0),//返回的容器是可变类型的(Array和Dictionary) NSJSONReadingMutableLeaves = (1UL << 1),//返回的叶子Nsstring是可变类型的; NSJSONReadingallowFragments = (1UL << 2) //允许顶层的界面不是NSArray或NSDictionary; }; typedef NSUInteger NSJSONReadingOptions;