2012-07-31 45 views
0

那麼,我決定是時候學習一門新技能,編程目標-c。我的編程知識非常少,它由PHP,一點HTML和一點點MySQL組成。我想要做的是採用一些使用php製作的JSON並填充iOS表格視圖。非常基本的JSON到tableView(iOS)

這裏是我的json.php:

<?php 
$array = array(1,2,3,4,5,6,7,8,9,10); 
$json = json_encode($array); 
echo $json; 
?> 

我唯一的問題是,是我不知道從哪裏開始如何使用JSONKit框架。 https://github.com/johnezang/JSONKit/我寧願將JSONKit用於其他替代方案,只是因爲它似乎具有更多的支持。

NSString *strURL = [NSString stringWithFormat:@"http://api.tekop.net/json.php"]; 
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]]; 
NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding]; 
NSLog(@"%@", strResult); 

我能夠在調試控制檯中打印JSON頁面的結果。現在我唯一的問題是接下來的問題?我試圖在網上尋找一些教程,但我能找到的是更先進的Jason phraser。提高我的基本需求。我知道我的問題非常模糊,可以給出很多不同的答案,但我認爲這是提出這些類型問題的地方。

回答

1

iOS有自己的JSON解析現在稱爲NSJSONSerialization。

http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html

你可以去看看。我總是這樣使用它。

[NSJSONSerialization JSONObjectWithData:tmpData 
             options:NSJSONReadingMutableContainers|NSJSONReadingAllowFragments 
             error:nil] 

樣品通過修改代碼:

NSString *strURL = [NSString stringWithFormat:@"http://api.tekop.net/json.php"]; 
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]]; 
NSArray *arrayResult = [NSJSONSerialization JSONObjectWithData:dataURL 
             options:NSJSONReadingMutableContainers|NSJSONReadingAllowFragments 
             error:nil]; 

NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding]; 

一些需要注意的是,如果你的JSON是一個基於陣列,使用的NSArray存儲結果。如果你的json是一個基於哈希的,使用NSDictionary。 (希望我說清楚,我不知道相關術語來形容)

0

你有什麼試過?

NSArray *array = [dataURL objectFromJSONData]; 
NSAssert([array isKindOfClass:[NSArray class]], @"This should be an array."); 

for (NSNumber *value in array) { 
    NSAssert([value isKindOfClass:[NSNumber class]], @"This should be a number."); 

    NSLog(@"%@", value); 
}