.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. List<int>是值类型还是引用类型

    class Program { static void Main(string[] args) { List<int> lst = new List<int>(); lst.A ...

  2. 如何在Root的手机上开启ViewServer,使得HierachyViewer能够连接

    前期准备: 关于什么是Hierarchy Viewer,请查看官方文档:http://developer.android.com/tools/debugging/debugging-ui.html.个 ...

  3. 普林斯顿大学算法课 Algorithm Part I Week 3 排序算法复杂度 Sorting Complexity

    计算复杂度(Computational complexity):用于研究解决特定问题X的算法效率的框架 计算模型(Model of computation):可允许的操作(Allowable oper ...

  4. 利用boost做string到wstring转换,以及字符集转换 - Error - C++博客

    利用boost做string到wstring转换,以及字符集转换 - Error - C++博客 利用boost做string到wstring转换,以及字符集转换 #include <boost ...

  5. 创业青年:刘霞(YBC推荐)_CCTV.com_中国中央电视台

    创业青年:刘霞(YBC推荐)_CCTV.com_中国中央电视台 创业青年:刘霞(YBC推荐) CCTV.com  2009年06月23日 09:57  进入复兴论坛  来源:央视网       姓名 ...

  6. Linux学习笔记1:配置Linux网络和克隆虚拟机并更改配置

    一.配置Linux网络 在安装Linux的时候,一定要保证你的物理网络的IP是手动设置的,要不然会在Linux设置IP连通网络的时候会报network is unreachable 并且怎么也找不到问 ...

  7. Log4Net不生成日志文件

    可能没有初始化配置,在Global文件Application_Start添加 log4net.Config.XmlConfigurator.Configure(); 或者输出日志进行初始化,如(Log ...

  8. 《github一天一道算法题》:插入排序

    看书.思考.写代码! /*********************************************** * copyright@hustyangju * blog: http://bl ...

  9. [置顶] 编辑框Editext光标最后显示

    [MainActivity] package com.example.testeditext; import android.os.Bundle; import android.app.Activit ...

  10. [Phonegap+Sencha Touch] 移动开发36 Phonegap/Cordova项目的图标和启动画面(splashscreen)配置

    原文地址:http://blog.csdn.net/lovelyelfpop/article/details/40780111 Phonegap/Cordova项目中的config.xml文件.里面配 ...