NSJSONSerialization

4 篇文章 0 订阅

以前一直用的是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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值