SBJson 左手的ㄟ右手 2023-10-18 13:24 70阅读 0赞 如何使用SBJson Json是一种类似XML的数据传输方式。详细介绍请看:[介绍JSON][JSON] [SBJson][]是与Objective-C结合比较好的库。 使用SBJson的文件需包含JSON.h头文件。 <table style="line-height:25px"> <tbody style="outline:none"> <tr style="outline:none"> <td style="outline:none; margin:0px; padding:0px 5px; word-break:break-all; word-wrap:break-word; height:20px; border-right-width:1px; border-right-style:solid; border-right-color:rgb(224,224,224); border-bottom-width:1px; border-bottom-style:solid; border-bottom-color:rgb(224,224,224)"> id jsonObject = [jsonString JSONValue];</td> </tr> </tbody> </table> 此句创建json对象,JSONValue自动将json字符内容初始化为json对象。当然先需要将json文件内容读取为字符串。jsonObject可能是NSDictionary或NSArray。具体根据json内容。 json内容被SBJson转换为Objective-C的类型的方式如下: <table style="line-height:25px"> <tbody style="outline:none"> <tr style="outline:none"> <td style="outline:none; margin:0px; padding:0px 5px; word-break:break-all; word-wrap:break-word; height:20px; border-right-width:1px; border-right-style:solid; border-right-color:rgb(224,224,224); border-bottom-width:1px; border-bottom-style:solid; border-bottom-color:rgb(224,224,224)"> Null -> NSNull<br style="outline:none"> String -> NSMutableString<br style="outline:none"> Array -> NSMutableArray<br style="outline:none"> Object -> NSMutableDictionary<br style="outline:none"> Boolean -> NSNumber<br style="outline:none"> Number -> NSDecimalNumber</td> </tr> </tbody> </table> Iphone利用JSON传递数据,展示在Table界面中 下面是一个最简单的例子。效果如图: ![201008161650.jpg][] 上面用到了json传递的数据,有关json部分,iphone sdk虽然没有支持,但是第三方已经写好了。 json 参考:[http://code.google.com/p/json-framework/][SBJson] 下面是具体的代码实现: 数据加载: #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: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”; 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]; [super dealloc]; } @end [JSON]: http://www.json.org/json-zh.html [SBJson]: http://code.google.com/p/json-framework/ [201008161650.jpg]: http://wangjun.easymorse.com/wp-content/uploads/2010/08/201008161650.jpg
相关 SBJson 如何使用SBJson Json是一种类似XML的数据传输方式。详细介绍请看:[介绍JSON][JSON] [SBJson][]是与Objective-C 左手的ㄟ右手/ 2023年10月18日 13:24/ 0 赞/ 71 阅读
相关 iphone 之SBJson 解析 第一种方式:使用老师的方法 快被坑死了 不过也意外的学到了一些别的方法 步骤一:新建工程 把SBJson的框架内容拉进工程 步骤二:新建或拉进工程一个资源文件 秒速五厘米/ 2022年08月08日 15:37/ 0 赞/ 202 阅读
相关 iOS 使用SBJSON创建和解析JSON 分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来![https://blog.csdn.net/jiangjuns 电玩女神/ 2022年03月17日 11:56/ 0 赞/ 255 阅读
还没有评论,来说两句吧...