SBJson
来源:互联网 发布:2017的网络流行语 编辑:程序博客网 时间:2024/05/22 17:08
如何使用SBJson
Json是一种类似XML的数据传输方式。详细介绍请看:介绍JSON
SBJson是与Objective-C结合比较好的库。
使用SBJson的文件需包含JSON.h头文件。
id jsonObject = [jsonString JSONValue];此句创建json对象,JSONValue自动将json字符内容初始化为json对象。当然先需要将json文件内容读取为字符串。jsonObject可能是NSDictionary或NSArray。具体根据json内容。
json内容被SBJson转换为Objective-C的类型的方式如下:
Null -> NSNull
String -> NSMutableString
Array -> NSMutableArray
Object -> NSMutableDictionary
Boolean -> NSNumber
Number -> NSDecimalNumber
SBJson是与Objective-C结合比较好的库。
使用SBJson的文件需包含JSON.h头文件。
id jsonObject = [jsonString JSONValue];此句创建json对象,JSONValue自动将json字符内容初始化为json对象。当然先需要将json文件内容读取为字符串。jsonObject可能是NSDictionary或NSArray。具体根据json内容。
json内容被SBJson转换为Objective-C的类型的方式如下:
Null -> NSNull
String -> NSMutableString
Array -> NSMutableArray
Object -> NSMutableDictionary
Boolean -> NSNumber
Number -> NSDecimalNumber
Iphone利用JSON传递数据,展示在Table界面中
下面是一个最简单的例子。效果如图:
上面用到了json传递的数据,有关json部分,iphone sdk虽然没有支持,但是第三方已经写好了。
json 参考:http://code.google.com/p/json-framework/
下面是具体的代码实现:
数据加载:
上面用到了json传递的数据,有关json部分,iphone sdk虽然没有支持,但是第三方已经写好了。
json 参考:http://code.google.com/p/json-framework/
下面是具体的代码实现:
数据加载:
#import “MyDataSource.h”#import “JSON.h”@implementation MyDataSource+ (NSDictionary *)fetchLibraryInformation{NSString *urlString = [NSString stringWithFormat:@"http://wangjun.easymorse.com/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:urlencoding:NSUTF8StringEncoding error:nil]; id jsonValue = [jsonString JSONValue];[jsonString release];return jsonValue;}@endtable数据展示:#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”;UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];if (cell == nil) {cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier: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];[super dealloc];}@end
- SBJson
- SBJson
- SBJson
- 使用SBJson
- SBJson用法
- SBJson库
- Sbjson教程
- SBJson解析
- Parsing JSON With SBJSON
- TouchJSON and SBJSON
- SBJson (aka json-framework)
- SBJson的使用。。
- SBJson的使用
- 如何使用SBJson
- sbjson的解析
- SBJSON 与tableview
- 关于SBJson库
- SBJSON的使用
- 深入理解硬链接和软链接
- 邮件发送类
- Linux ls详细解释
- String s; String s = null; String s = ""; .intern()
- android解析XML总结(SAX、Pull、Dom三种方式)
- SBJson
- Linux history时间用户ip设置
- 数据库开发及ADO.NET学习笔记(一)
- ubuntu下安装eclipse并配置启动命令行
- Linux 注册系统服务
- 九度OJ 题目1169:比较奇偶数个数
- JS:《Microsoft JScript 运行时错误: 缺少 '}'》解决方案
- 寒假刷题之7——波纹
- boost库在工作(4)作用域智能指针scoped_ptr之三