如何使用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
 
Iphone利用JSON傳遞數據,展示在Table界面中

下面是一個最簡單的例子。效果如圖:

上面用到了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 allocinitWithContentsOfURL: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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章