iOS开发 json在iOS上的使用
来源:互联网 发布:手机淘宝店铺免费模板 编辑:程序博客网 时间:2024/06/05 17:22
在iPhone上访问网络内容是很必须的,而一些数据就需要以某种形式储存在web服务器上。比如一个app的目录,内容,索引等等。而xml和json,plist都是比较方便的方式。
-XML在iPhone上是非常好用的,但是对复杂的数据结构使用上就不那么方便了,具体可以参阅苹果的“基于事件的XML”和“基于树的XML”编程向导
-plist是再方便不过了,不过我看最多也就是一个NSDictionary而已,复杂一些的话,数据输入上也会非常非常的麻烦。
-JSON本来是不被苹果支持的,但是有人很Nice的帮我们解决了这个问题:JSON for OBJC http://code.google.com/p/json-framework/
基本上来说,这个框架异常的简单易用,会将得到的json字符串处理成一个复杂NSDictionary对象,而每一个值都还是一个NSDictionary对象
比如:
{ "华藏净宗学会": { "zhaomu": { "name":"净宗朝暮课本", "length":142, "digits":3 }, "kesong": { "name":"净宗共修课本", "length":75, "digits":2 } }, "生命基金会": { "dabei88": { "name":"大悲出相图", "length":88, "digits":2 } }}
就会转换为一个复杂无比的NSDictionary:
[[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects: [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects: [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects: @"净宗朝暮课本", @"142", @"3",nil] forKeys: [NSArray arrayWithObjects: @"name", @"length", @"digits",nil]], [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects: @"净宗共修课本", @"75", @"2",nil] forKeys: [NSArray arrayWithObjects: @"name", @"length", @"digits",nil]],nil] forKeys:[NSArray arrayWithObjects:@"zhaomu",@"kesong",nil]], [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects: [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects: @"大悲出相图", @"88", @"2",nil] forKeys: [NSArray arrayWithObjects: @"name", @"length", @"digits",nil]],nil] forKeys:[NSArray arrayWithObjects:@"dabei88",nil]],nil] forKeys:[NSArray arrayWithObjects:@"华藏净宗学会",@"生命基金会",nil]];
没有编译错误
不管怎么样,转换后,在系统中就可以非常方便的使用json的键值结构信息咯~!!!
简单介绍一下使用。
列出了以下函数:
+ (id)fetchJSONValueForURL:(NSURL *)url{ NSString *jsonString = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil]; id jsonValue = [jsonString JSONValue]; [jsonString release]; return jsonValue;}
基本上这个函数方法就是将一个传入的互联网地址,当然是一个.json文件的位置。然后传出的就是一个NSDictionary~
我在使用的时候,写了一个方法来调用它:
+ (NSDictionary *)fetchLibraryInformation{ NSString *urlString = [NSString stringWithFormat:@"http://gzl.name/appData/MyFodian/Lib/main.json"]; NSURL *url = [NSURL URLWithString:urlString]; NSLog(@"fetching library data"); return [self fetchJSONValueForURL:url];}
这个方法不过是将我存放json文件的地址告诉了前面的方法,然后返回这个NSDictionary,这回的返回值我用得 NSDictionary,其实和id在这里没有区别。记得在使用这个对象的时候还要retain一下,否则一会儿就没有咯。因为JSONValue方法 返回的应该是一个autorelease对象。
接下来我们就可以使用这个NSDictionary中的数据拉!
第一期请参看iPhone上的JSON
既然JSON这么好,它怎么和UITableView结合使用呢?
首先看看我们的JSON文件吧:
{ "老张家":["大张","二张","三张"], "老李家":["大李","二李"]}
开始打代码吧。
1,首先copy JSON库到当前的Project里面。
2,建立一个数据源类。我给它起名叫MyDataSource, 看看里面都有什么吧:
@interface MyDataSource : NSObject {}+ (id)dataSource;@end #import "JSON.h" @implementation MyDataSource+ (id)dataSource{ NSString* JSONString = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"data" ofType:@"json"] encoding:NSUTF8StringEncoding error:nil]; return [JSONString JSONValue];}@end
里面非常简单,只有一个类方法dataSource。在其中我们读取json文件的内容到一个NSString中,并用JSON框架来解读成一个 NSDictionary,返回值为id。因为虽然大多的时候最外的对象都为NSDictionary,但是出于严谨,万一是NSArray不就崩溃了。 所以使用id,这样其实就有再次可以用的特性了。
3,建立一个UITableViewController, 然后作适当的设置:
#import "MyTableViewController.h"#import "MyDataSource.h" @implementation MyTableViewController - (id)initWithStyle:(UITableViewStyle)style{ if (self = [super initWithStyle:style]) { myData = [[MyDataSource dataSource] retain]; //在这里我们初始化myData,其实就是一个id对象 //传入由MyDataSource解析出的NSDictionary } return self;} #pragma mark Table view methods - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return [myData count]; //有多少个section,也就是“几家”} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [[myData valueForKey:[[myData allKeys] objectAtIndex:section]] count];//这里我们需要告诉UITableViewController每个section里面有几个,也就是“一家里面有几口人”} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } //上面的东西都是重复白给的,平时没事不用想为什么,照抄就可以了
cell.textLabel.text = [[myData valueForKey:[[myData allKeys] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
//这句看上去复杂,但是其实不过是在特定section里面找到对应的array, //然后在array中找到indexPath.row所在的内容 return cell;} - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ return [[myData allKeys] objectAtIndex:section];//这里设置对应section的名字,很简单allKey返回所有的键值为一个array,也就是“张家”,“李家”//然后用objectAtIndex: 来找出究竟是哪一个就可以了!} - (void)dealloc { [myData release]; //“我们是runtime的好市民”...release就好Alan...... [super dealloc];}@end
4,在主程序代理 xxxAppDelegate 里面初始化这个UITableViewController然后添加它的view到window的subview中就OK拉!
5,编译运行,没有错误就万事ok
- iOS开发 json在iOS上的使用
- JSON在 ios上的使用
- JSON在 ios上的使用
- iOS开发小记:iOS富文本框架DTCoreText在UITableView上的使用
- POCO C++ 在IOS上的使用
- Protobuf在ios上的使用
- iconFont在iOS上的使用详解
- 使用 Xcode 在越狱 iOS 设备上进行开发调试
- 使用 Xcode 在越狱 iOS 设备上进行开发调试
- 使用 Xcode 在越狱 iOS 设备上进行开发调试
- 使用 Xcode 在越狱 iOS 设备上进行开发调试
- 使用 Xcode 在越狱 iOS 设备上进行开发调试
- iOS开发证书在多台机器上使用
- iOS开发证书在多台机器上使用
- 在ios上使用ffmpeg
- 在iOS上使用ffmepg
- 在iOS上使用ffmepg
- 在iOS上使用ffmepg
- 六角星的创建
- 【甘道夫】Spark1.3.0 Running Spark on YARN 官方文档精华摘要
- OpenStack Neutron运行机制解析概要
- Code Snippets
- 微信支付证书问题C#使用Https访问加载SSL证书本地调试正常服务器IIS内部错误解决方法
- iOS开发 json在iOS上的使用
- 升级openssl
- linux 3 与 linux2.6 的ioctl区别
- dev_set_drvdata,dev_get_drvdata
- svn图标不显示的解决方案
- mysql账号权限密码设置方法
- 封装转换
- form表单禁止button 提交
- WEBAPP开发技巧总结