NSJSONSerialization(category)的一個擴充類

來源:互聯網
上載者:User

標籤:des   blog   檔案   2014   cti   html   

.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, 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 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, if applicable. * @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 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, if applicable. * @return NSString formatted as JSON, 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



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.