您的位置:首页 > 移动开发 > Objective-C

Objective - c JsonKit 进行json转对象 对象转json数据 & Jastor json数据转为对象

2014-05-07 18:07 316 查看
在ios平台做通用的json数据解析,直接将json格式字符串转化成 对应的Object类(比如:jsonUser 转 User对象)。

思路: 1、 获取服务器端的json数据,然后解析成NSDictionary对象(我们一般是使用第三方的json解析工具JSONKit,SBJson等)。

2、 使用第三方工具Jastor将NSDictionary 转成 响应Object对象。(Jastor下载和用法参考https://github.com/elado/jastor,在该地址下载下Jastor包后,直接将Jastor文件夹拉到ios项目中,只有4个文件Jastor.h、Jastor.m、JastorRuntimeHelper.h、JastorRuntimeHelper.m)

开始编码:

You have the following JSON:

json数据

{
"name": "Foo",
"amount": 13
}


定义对象

@interface Product
@property (nonatomic, copy) NSString *name;
@property (nonatomic, retain) NSNumber *amount;
@end

@implementation Product
@synthesize name, amount;

- (void)dealloc {
self.name = nil;
self.amount = nil;

[super dealloc];
}
@end


// Product.m
@implementation Product
@synthesize name, amount;

- (void)dealloc {
self.name = nil;
self.amount = nil;

[super dealloc];
}
@end

// Some other code
NSDictionary *dictionary = /* parse the JSON response to a dictionary */;
Product *product = [[Product alloc] initWithDictionary:dictionary];

// Log
product.name // => Foo
product.amount // => 13


更多节点。。。

// JSON
{
"name": "Foo",
"category": {
"name": "Bar Category"
}
}


// ProductCategory.h
@interface ProductCategory : Jastor
@property (nonatomic, copy) NSString *name;
@end

// ProductCategory.m
@implementation ProductCategory
@synthesize name;

- (void)dealloc {
self.name = nil;

[super dealloc];
}
@end

// Product.h
@interface Product : Jastor
@property (nonatomic, copy) NSString *name;
@property (nonatomic, retain) ProductCategory *category;
@end

// Product.m
@implementation Product
@synthesize name, category;

- (void)dealloc {
self.name = nil;
self.category = nil;

[super dealloc];
}
@end

// Code
NSDictionary *dictionary = /* parse the JSON response to a dictionary */;
Product *product = [[Product alloc] initWithDictionary:dictionary];

// Log
product.name // => Foo
product.category // => <ProductCategory>
product.category.name // => Bar Category


NSArray...

// JSON
{
"name": "Foo",
"categories": [
{ "name": "Bar Category 1" },
{ "name": "Bar Category 2" },
{ "name": "Bar Category 3" }
]
}


// ProductCategory.h
@interface ProductCategory : Jastor
@property (nonatomic, copy) NSString *name;
@end

// ProductCategory.m
@implementation ProductCategory
@synthesize name;

- (void)dealloc {
self.name = nil;

[super dealloc];
}
@end

// Product.h
@interface Product : Jastor
@property (nonatomic, copy) NSString *name;
@property (nonatomic, retain) NSArray *categories;
@end

// Product.m
@implementation Product
@synthesize name, categories;

+ (Class)categories_class {
return [ProductCategory class];
}

- (void)dealloc {
self.name = nil;
self.categories = nil;

[super dealloc];
}
@end

// Code
NSDictionary *dictionary = /* parse the JSON response to a dictionary */;
Product *product = [[Product alloc] initWithDictionary:dictionary];

// Log
product.name // => Foo
product.categories // => <NSArray>
[product.categories count] // => 3
[product.categories objectAtIndex:1] // => <ProductCategory>
[[product.categories objectAtIndex:1] name] // => Bar Category 2


+ (Class)categories_class {
return [ProductCategory class];
}


JSON和Object-C中数据类型的映射关系如下表所示

JSONObjective-C
null
NSNull
true
and
false
NSNumber
Number
NSNumber
String
NSString
Array
NSArray
Object
NSDictionary
使用JSONKit 进行加密解密操作

#import "JSONKit.h"

@interface BYViewController ()

@end

@implementation BYViewController

- (void)viewDidLoad
{
[super viewDidLoad];

NSString *res = nil;

/*
* json格式编码 对象 转json  对象加密成json数据
*/

//字符串   字符串转json
NSString *str = @"this is a nsstring";
res = [str JSONString];
NSLog(@"res= %@", [NSString stringWithString: res]);
//res= "this is a nsstring"

//数组     数组 转 json
NSArray *arr = [[NSArray alloc] initWithObjects:@"One",@"Two",@"Three",nil];
res = [arr JSONString];
NSLog(@"res= %@", [NSString stringWithString: res]);
//res= ["One","Two","Three"]

//字典类型(对象)  对象 转 json
NSArray *arr1 = [NSArray arrayWithObjects:@"dog",@"cat",nil];
NSArray *arr2 = [NSArray arrayWithObjects:[NSNumber numberWithBool:YES],[NSNumber numberWithInt:30],nil];
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:arr1,@"pets",arr2,@"other",nil];
res = [dic JSONString];
NSLog(@"res= %@", [NSString stringWithString: res]);
//res= {"pets":["dog","cat"],"other":[true,30]}

/*
* json格式解码    json 转对象   json解密为对象
*/
JSONDecoder *jd=[[JSONDecoder alloc] init];

//针对NSData数据     json 转nsData
NSData *data = [dic JSONData];
NSDictionary *ret = [jd objectWithData: data];
NSLog(@"res= %@", [ret objectForKey:@"pets"]);
//res= (
//  dog,
//  cat
//)
NSLog(@"res= %@", [[ret objectForKey:@"other"] objectAtIndex:0]);
//res= 1

//针对NSString字符串数据      json 转nsstring
NSString *nstr = [dic JSONString];
NSDictionary *ret2 = [jd objectWithUTF8String:(const unsigned char *)[nstr UTF8String] length:(unsigned int)[nstr length]];
NSLog(@"res= %d", [[ret2 objectForKey:@"pets"] indexOfObject:@"cat"]);
//res= 1
NSLog(@"res= %@", [[ret2 objectForKey:@"other"] objectAtIndex:1]);
//res= 30
}
@end





                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐