SBJson库解析(二)NSObject+SBJson
2012-02-17 16:03
267 查看
一.NSObject+SBJson.h
1.把objc对象编码成json字符串
通过类别,为NSObject添加新方法:[NSObject JSONRepresentation]
2.把json对象解析为objc对象
通过类别,为NSString添加新方法:[NSString JSONValue]
二.NSObject+SBJson.m
导入头文件
1.通过json编写器SBJsonWriter,的stringWithObject: 方法,实现[NSObject JSONRepresentation]编码逻辑
2.通过json解析器SBJsonParser,的objectWithString: 方法,实现[NSString JSONValue]解析逻辑
1.把objc对象编码成json字符串
通过类别,为NSObject添加新方法:[NSObject JSONRepresentation]
@interface NSObject (NSObject_SBJsonWriting) /** 虽然定义成NSObject的类别,但仅对NSArray和NSDictionary有效 返回:已编码的json对象,或nil */ - (NSString *)JSONRepresentation; @end
2.把json对象解析为objc对象
通过类别,为NSString添加新方法:[NSString JSONValue]
@interface NSString (NSString_SBJsonParsing) /** 返回:NSDictionary或NSArray对象,或nil */ - (id)JSONValue; @end
二.NSObject+SBJson.m
导入头文件
#import "NSObject+SBJson.h" #import "SBJsonWriter.h" #import "SBJsonParser.h"
1.通过json编写器SBJsonWriter,的stringWithObject: 方法,实现[NSObject JSONRepresentation]编码逻辑
@implementation NSObject (NSObject_SBJsonWriting) //objc2json - (NSString *)JSONRepresentation { SBJsonWriter *writer = [[SBJsonWriter alloc] init]; NSString *json = [writer stringWithObject:self]; if (!json) NSLog(@"-JSONRepresentation failed. Error is: %@", writer.error); return json; } @end
2.通过json解析器SBJsonParser,的objectWithString: 方法,实现[NSString JSONValue]解析逻辑
@implementation NSString (NSString_SBJsonParsing) //json2objc - (id)JSONValue { SBJsonParser *parser = [[SBJsonParser alloc] init]; id repr = [parser objectWithString:self]; if (!repr) NSLog(@"-JSONValue failed. Error is: %@", parser.error); return repr; } @end
相关文章推荐
- SBJson库解析(三)SBJsonWriter
- SBJson库解析(八)SBJsonStreamParserAdapter
- iOS源码解析—YYModel(NSObject+YYModel)
- iOS 使用SBJSON创建和解析JSON
- SBJson库解析(九)SBJsonStreamParser
- iOS json解析的几种方法 NSJSONSerialization,JSONKit,SBJson ,TouchJson
- SBJson库解析(十)SBJsonStreamParserAccumulator
- iOS 基础类解析 - NSObject
- NSObject头文件解析 / 消息机制 / Runtime解读 (一)
- iOS json解析之SBJson
- iOS 使用SBJSON创建和解析JSON
- ios 中使用SBJson拼接和解析json
- iOS 使用SBJSON创建和解析JSON
- ios开发之解析json字符串(SBJson)
- ASIHttpRequest+SBJson完成Http请求,以及json数据解析
- NSObject头文件解析 - 消息机制 - Runtime解读 (二)
- ios 中使用SBJson拼接和解析json
- NSObject深度解析
- ios 中使用SBJson拼接和解析json
- sbjson的解析