- 3
- 0
- 约2.87千字
- 约 4页
- 2016-07-05 发布于重庆
- 举报
文档-利用JSON传递数据
iphone利用JSON传递数据
json不必多说了,在手机开发中,客户端和服务器端开发经常使用的数据交互模式。Iphone中利用json传递的数据,可以非常容易的展示到各个容器中。下面是一个最简单的例子。效果如图:
上面用到了json传递的数据,有关json部分,iphone sdk虽然没有支持,但是第三方已经写好了。
json 参考:/p/json-framework/
下面是具体的代码实现:
数据加载:
? ? #import “MyDataSource.h”? ? #import “JSON.h”? ? @implementation MyDataSource? ? + (NSDictionary *)fetchLibraryInformation? ? {? ? NSString *urlString = [NSString stringWithFormat:@/wp-content/video/hello.jison];? ? NSURL *url = [NSURL URLWithString:urlString];? ? NSLog(@”fetching library data”);? ? return [self fetchJSONValueForURL:url];? ? }? ? + (id)fetchJSONValueForURL:(NSURL *)url? ? {? ? NSString *jsonString = [[NSString alloc] initWithContentsOfURL:url? ? encoding:NSUTF8StringEncoding error:nil];? ? id jsonValue = [jsonString JSONValue];? ? [jsonString release];? ? return jsonValue;? ? }? ? @end
table数据展示:
? ? #import “JSONTableTestViewController.h”? ? #import “MyDataSource.h”? ? @implementation JSONTableTestViewController? ? @synthesize myData;? ? - (void)viewDidLoad {? ? NSLog(@”加载数据“);? ? myData = [[MyDataSource fetchLibraryInformation] retain];? ? }? ? - (void)didReceiveMemoryWarning {? ? // Releases the view if it doesn’t have a superview.? ? [super didReceiveMemoryWarning];? ? // Release any cached data, images, etc that aren’t in use.? ? }? ? - (void)viewDidUnload {? ? // Release any retained subviews of the main view.? ? // e.g. self.myOutlet = nil;? ? }? ? - (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
您可能关注的文档
最近下载
- 来院人员工资核定表.doc VIP
- 2025年换电模式下的电动汽车充电设施布局与运营优化研究报告.docx VIP
- 2025年充电与换电:新能源汽车换电模式与充电设施协同发展研究报告.docx VIP
- 2025年新疆维吾尔自治区高考政治真题含解析.docx VIP
- 混合能源微电网经济调度方法研究.docx VIP
- 中国人民大学助教岗位申请表.doc
- 重卡汽车换电基础设施项目规划设计方案.pptx VIP
- 微网经济调度问题的混合整数规划方法.pdf VIP
- 2025年换电模式下的电动汽车充电设施运营与管理研究报告.docx VIP
- 2025年新能源汽车换电模式供应链协同效率分析报告.docx VIP
原创力文档

文档评论(0)