2013-02-12 140 views
0

我已經嘗試了這幾次,我仍然沒有得到如何進入JSON供稿並檢索我需要抓取的內容。Json Iphone解析數據

飼料看起來像這樣,在我的代碼我試圖拉出所有的標題。我不知道如何進入Json Feed。

enter image description here

- (void)viewDidLoad 
    { 
     [super viewDidLoad]; 


     [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; 

     NSURL *url = [NSURL URLWithString:@"http://api.storageroomapp.com/accounts/511a4f810f66026b640007b8/collections/511a51580f66023bff000ce9/entries.json?auth_token=Zty6nKsFyqpy7Yp5DP1L&preview_api=1"]; 

     NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
     [[NSURLConnection alloc] initWithRequest:request delegate:self]; 



     // Do any additional setup after loading the view from its nib. 
    } 

    -(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ 
     data = [[NSMutableData alloc] init]; 
    } 
    -(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData{ 
     [data appendData:theData]; 
    } 
    -(void)connectionDidFinishLoading:(NSURLConnection *) connection{ 
     [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 

     news = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil]; 
     [mainTableView reloadData]; 
    } 

    -(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ 
     UIAlertView *errorView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"The data could not be downloaded - please make sure you're connected to either 3G or Wi-FI" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil]; 
     [errorView show]; 
     [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 
    } 


    -(int)numberOfSectionsInTableView:(UITableView *)tableView{ 
     return 1; 
    } 
    -(int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
     return [news count]; 
    } 

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"]; 
     if (cell == nil) { 
      cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MainCell"]; 


      cell.textLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:@"title"]; 

     } 

     return cell; 
    } 

,並在我的.H我有NSArray的*新聞;和NSMutableData *數據。

任何幫助都會很棒,你能否清楚地解釋你的自我,因爲我是這個語言的全新手。

回答

2

看看您在代碼中的邏輯以及您發佈的示例JSON,它看起來不像您的數組正在填充您想要的內容。

最初,JSON採用字典的形式(因此您提供圖像中的花括號)。因此,您應該將JSON的初始解析調整爲如下所示:

NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil]; 

從這裏,您可以從字典中接收密鑰。你要找的是「數組」,儘管它的名字,它實際上也是一本字典。

NSDictionary *arrayDictionary = dictionary[@"array"]; 

權一起移動,你終於可以訪問你正在尋找的「資源」陣列,並且可以存儲您在.h文件中創建的實例變量中。

news = arrayDictionary[@"resources"]; 

現在,在cellForRowAtIndexPath方法你可以訪問此陣列基於被提供給您的指數路徑行的各種元件。

NSDictionary *newsItem = news[[indexPath row]]; 

最後,您可以訪問各種屬性,如來自該新聞項目的標題,並設置文本標籤的文本。

NSString *title = newsItem[@"title"]; 
[[cell textLabel] setText:title]; 
+0

嗨,如果我上傳的zip可以快速查找,發現它很難遵循。 – Brent 2013-02-12 17:15:45

+0

好吧,看看你在解析字典,並根據我在帖子中說的來調整它。最後,我還指出你需要訪問標題的方法和邏輯。給它一個去,如果你有任何問題,迴應。 – 2013-02-12 17:24:46

+0

對不起,我不明白最後一點? – Brent 2013-02-12 17:28:49