.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

option參数说明.

     enum {

        NSJSONReadingMutableContainers = (1UL << 0),   //返回的容器是可变类型的(Array和Dictionary)

        NSJSONReadingMutableLeaves = (1UL << 1),     //返回的叶子NSString是可变类型的;

        NSJSONReadingAllowFragments = (1UL << 2)   //同意顶层的界面不是NSArray或NSDictionary;

    };

    typedef NSUInteger NSJSONReadingOptions;

NSJSONSerialization(category)的一个扩展类的更多相关文章

  1. [ios]objective-c中Category类别(扩展类)专题总结

    本文转载至 http://yul100887.blog.163.com/blog/static/20033613520126333344127/   objective-c类别的作用?通过类别的方式, ...

  2. ios 中Category类别(扩展类)小结

    类别 类别是一种为现有的类添加新方法的方式.利用Objective-C的动态运行时(runtime)分配机制,可以为现有的类添加新方法,这种为现有的类添加新方法的方式称为类别catagory,他可以为 ...

  3. iOS的扩展类,扩展属性

    Objective-C有两个扩展机制:Associative和Category.Category用来扩展类方法,Associative用于扩展属性.Associative机制的原理是把两个对象关联起来 ...

  4. C# 扩展类

    C# 中提供一个非常实用的供能,扩展方法(Extension method) 扩展方法是通过额外的静态方法扩展现有的类型.通过扩展方法,可以对已有类型做自己想做的相关扩展.方法:定义静态类,扩展方法也 ...

  5. 怎样从一个DLL中导出一个C++类

    原文作者:Alex Blekhman    翻译:朱金灿 原文来源: http://www.codeproject.com/KB/cpp/howto_export_cpp_classes.aspx 译 ...

  6. Java+7入门经典 - 6 扩展类与继承 Part 1/2

    第6章 扩展类与继承 面向对象编程的一个重要特性: 允许基于已定义的类创建新的类; 6.1 使用已有的类 派生 derivation, 派生类 derived class, 直接子类 direct s ...

  7. Thinkphp编辑器扩展类kindeditor用法

    一, 使用前的准备. 使用前请确认你已经建立好了一个Thinkphp站点项目. 1,Keditor.class.php和JSON.class.php 是编辑器扩展类文件,将他们拷贝到你的站点项目的Th ...

  8. struts1.x 核心控制器 和 用户自定义控制器扩展类;

    ServletAction继承于HttpServlet,是struts1.x中和核心控制器. 配置于web.xml文件中,指定config属性,该config属性用于指定formBean和action ...

  9. 颜色扩展类--ColorExtensions

    /// <summary> /// 颜色扩展类 /// </summary> public static class ColorExtensions { /// <sum ...

随机推荐

  1. DIV+CSS规范命名

    一.命名规则说明: 1).所有的命名最好都小写2).属性的值一定要用双引号("")括起来,且一定要有值如class="divcss5",id="div ...

  2. [开源]在iOS上实现Android风格的控件Toast

    [开源]在iOS上实现Android风格的控件Toast iOS的风格和Apple其他产品一样,简单而粗暴.没有给人其他选择的余地,让你又爱又恨.同样的,Apple对待iOS平台的开发人员和对待大众消 ...

  3. HealthKit框架参考(转)

    来自:http://www.cocoachina.com/ios/20140915/9624.html 本文由CocoaChina翻译组成员 唧唧歪歪(微博) 翻译自苹果官方文档:The Health ...

  4. js中 ===与==

    js里面,==比较的是参数的值,不会比较参数的类型,===需要先比较参数的类型是否一致,然后才会去比较值比如,if(3 == "3")这个会返回true,if(3 === &quo ...

  5. 在 PL/SQL Developer 中执行SQL文件的方法

    打开 command Window SQL> @'D:\My Documents\Downloads\bde_chk_cbo.sql'; 整个路径及文件两边要有单引号哦!

  6. iOS技术

    iOS技术 OC:分类(好处,和延展的区别) 分类: 在不修改原有的类的基础上增加新的方法  一个庞大的类可以分模块开发 一个庞大的类可以由多个人来编写,更有利于团队合作 分类是对原有类的一种扩展,在 ...

  7. C#:读取配置文件

    以下代码演示如何读取配置文件---------------------Factory.cs----------------------------using System;using System.C ...

  8. 加密传输SSL协议4_综合方案

    隔了那么多天终于有时间继续把这个专题做完了,这次一定连续写完这方面的笔记. 上篇博文说明了非对称加密和对称加密各自的优缺点,那么就很自然的衍生出了一种综合的方案. 两种方案的结合--扬长避短 首先发送 ...

  9. struts2中使用ognl表达式时各种符号的使用规则$,#,%

    OGNL表达式struts2标签“%,#,$” 一.什么是OGNL,有什么特点? OGNL(Object-Graph Navigation Language),大概可以理解为:对象图形化导航语言.是一 ...

  10. Python封装的访问MySQL数据库的类及DEMO

    # Filename:mysql_class.py # Author:Rain.Zen; Date: 2014-04-15 import MySQLdb class MyDb: '''初始化[类似于构 ...