`
啸笑天
  • 浏览: 3462532 次
  • 性别: Icon_minigender_1
  • 来自: China
社区版块
存档分类
最新评论

NSJSONSerialization

 
阅读更多

以前一直用的是SBJSON!NSJSONSerialization是5.0开始提供的,关于和其他的开源库的解析速度对比:http://arthurchen.blog.51cto.com/2483760/723910。

NSJSONSerialization  文档:https://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html

note:两个枚举常量意义:

NSJSONReadingOptions

Options used when creating Foundation objects from JSON data—see JSONObjectWithData:options:error: andJSONObjectWithStream:options:error:.

enum {
   NSJSONReadingMutableContainers = (1UL << 0),
   NSJSONReadingMutableLeaves = (1UL << 1),
   NSJSONReadingAllowFragments = (1UL << 2)
};
typedef NSUInteger NSJSONReadingOptions;
Constants

NSJSONReadingMutableContainers

Specifies that arrays and dictionaries are created as mutable objects.

Available in iOS 5.0 and later.

Declared in NSJSONSerialization.h.

NSJSONReadingMutableLeaves

Specifies that leaf strings in the JSON object graph are created as instances of NSMutableString.

Available in iOS 5.0 and later.

Declared in NSJSONSerialization.h.

NSJSONReadingAllowFragments

Specifies that the parser should allow top-level objects that are not an instance of NSArray or NSDictionary.

Available in iOS 5.0 and later.

Declared in NSJSONSerialization.h.

NSJSONWritingOptions

Options for writing JSON data.

enum {     NSJSONWritingPrettyPrinted = (1UL << 0) }; typedef NSUInteger NSJSONWritingOptions;
Constants

NSJSONWritingPrettyPrinted

Specifies that the JSON data should be generated with whitespace designed to make the output more readable. If this option is not set, the most compact possible JSON representation is generated.

Available in iOS 5.0 and later.

Declared in NSJSONSerialization.h.

 

例子:

//
//  EASYJSONCategories.h
//  EASYJSON
//
//  Created by EZ on 13-5-23.
//  Copyright (c) 2013年 cactus. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface NSObject (EASYJSONCategories)
- (NSString *)prettyJSONString;
- (NSString *)JSONStringRepresentation;
@end

@interface NSString (EASYJSONCategories)
- (id)JSONObject;
@end

@interface NSData (EASYJSONCategories)
- (id)JSONData;
@end

 

//
//  EASYJSONCategories.m
//  EASYJSON
//
//  Created by EZ on 13-5-23.
//  Copyright (c) 2013年 cactus. All rights reserved.
//

#import "EASYJSONCategories.h"

@implementation NSObject (EASYJSONCategories)

-(NSString*) JSONStringWithOption:(int) option
{
    if(self == nil){
        return nil;
    }
    NSError *err = nil;
    NSData *data = [NSJSONSerialization dataWithJSONObject:self
                                                   options:option
                                                     error:&err];
    
    if(err)
        NSLog(@"%@", [err description]);
    
    return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
}

- (NSString *)prettyJSONString {
    return [self JSONStringWithOption:NSJSONWritingPrettyPrinted];
}

- (NSString *)JSONStringRepresentation {
    return [self JSONStringWithOption:0];
}

@end


@implementation NSString (EASYJSONCategories)

- (id)JSONObject {
    if(self == nil){
        return self;
    }
    NSError *err = nil;
    NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding];
    id jsonValue = [NSJSONSerialization JSONObjectWithData:data
                                                   options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves
                                                     error:&err];
    
    if(err)
        NSLog(@"%@", [err description]);
    return jsonValue;
}

@end

@implementation NSData (EASYJSONCategories)
- (id)JSONData
{
    if(self == nil){
        return nil;
    }
    NSError *err = nil;
    id jsonValue = [NSJSONSerialization JSONObjectWithData:self
                                                   options:0
                                                     error:&err];
    if(err)
        NSLog(@"%@", [err description]);
    return jsonValue;
}

@end

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics