2013-01-12 26 views
2

我想解析來自reddit.com的JSON飼料,然後顯示它在tableview中。該JSON看起來是這樣的:獲取reddit飼料作爲JSON和解析它

{ 
kind: Listing 
data: { 
modhash: 3lmt2d4hq6797af804da91bde566bd067ddee68e1e7f8e0dda 
children: [ 
{ 
kind: t3 
data: { 
domain: imgur.com 
banned_by: null 
media_embed: { } 
subreddit: funny 
selftext_html: null 
selftext: 
likes: null 
link_flair_text: null 
id: 16e9so 
clicked: false 
title: I left my dog unattended with the 2 year old... 
num_comments: 166 
score: 2213 
approved_by: null 
over_18: false 
hidden: false 
thumbnail: http://c.thumbs.redditmedia.com/DaoiOlvtdjaPBG3n.jpg 
subreddit_id: t5_2qh33 
edited: false 
link_flair_css_class: null 
author_flair_css_class: null 
downs: 2481 
saved: false 
is_self: false 
permalink: /r/funny/comments/16e9so/i_left_my_dog_unattended_with_the_2_year_old/ 
name: t3_16e9so 
created: 1357963292 
url: http://imgur.com/zGMxP 
author_flair_text: null 
author: ShaneNickerson 
created_utc: 1357934492 
media: null 
num_reports: null 
ups: 4694 
} 
...more items here 
} 

而且這裏是我的tableview控制器代碼:

#import "mainRedditFunViewController.h" 
    #define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) 

    #define kjsonURL [NSURL URLWithString: @"http://reddit.com/r/funny/.json" 
@interface mainRedditFunViewController() 

@end 

@implementation mainRedditFunViewController 

- (void)viewDidLoad 

{ 

    [super viewDidLoad]; 

    dispatch_async(kBgQueue, ^{ 

     NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://reddit.com/r/funny/.json"]]; 

     [self performSelectorOnMainThread:@selector(fetchedData:) 
           withObject:data waitUntilDone:YES]; 

    }); 

} 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 

{ 

    // Return the number of sections. 

    return 1; 

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

{ 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] ; 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    } 

    NSDictionary *appsdict = [jsonResults objectAtIndex:indexPath.row]; 

    NSString *VersionString = [appsdict objectForKey:@"url"]; 

    NSString *priceString = [appsdict objectForKey:@"author"]; 

    NSURL *imageURL = [NSURL URLWithString:[appsdict objectForKey:@"thumbnail"]]; 

    NSData *imageData = [NSData dataWithContentsOfURL:imageURL]; 

    UIImage *imageLoad = [[UIImage alloc] initWithData:imageData]; 

    cell.textLabel.text = [appsdict objectForKey:@"title"]; 

    cell.detailTextLabel.text = [NSString stringWithFormat:@"URL: %@ Author: $ %@ USD",VersionString,priceString]; 

    cell.imageView.image = imageLoad; 

    return cell; 

} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 

{ 

    // Return the number of rows in the section. 

    return [jsonResults count]; 

} 


- (void)fetchedData:(NSData *)responseData { 

    NSError* error; 

    NSDictionary* json = [NSJSONSerialization 

          JSONObjectWithData:responseData 

          options:kNilOptions 

          error:&error]; 

    jsonResults = [json objectForKey:@"data.children"]; 

    [self.tableView reloadData]; 

} 
- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

@end 

但是,輸出是完全地空。無論如何我可以修復它?我之前嘗試過使用另一個(較小)的Feed,它運行良好。我敢肯定,如果我做的是正確JSONPath格式化,因爲我想看看這樣的:

-kind 
->data 
    ->children 
    ->data (my items) 
    ->data 
    ->data 
    ... 

我會很感激所有幫助我搞定!

回答

1

對不起,這個答案很晚!希望它仍然需要...

我已經改變了你的代碼一點點來訪問返回的Json結果的子級別。

你當然有正確的想法,但只需要你的fetchedData方法中的幾個字典。如果你在你的NSJSONSerialization下面添加這些字典的方法;

NSDictionary* json = [NSJSONSerialization 

         JSONObjectWithData:data 

         options:kNilOptions 

         error:&error]; 

//NSArray *Fullarray = [[NSArray alloc] initWithObjects:json, nil]; 

_jsonResult = [[NSMutableArray alloc] init]; 

NSDictionary *dataDict = [json objectForKey:@"data"]; 
NSDictionary *chilrenDict = [dataDict objectForKey:@"children"]; 

for (NSDictionary *informationFromChild in chilrenDict) { 
    NSLog(@"chil %@", chilrenDict); 
    [_jsonResult addObject:chilrenDict]; 
} 

然後在您的tableView cellForRow你appsDict改變這種

NSDictionary *appsdict = [[_jsonResult objectAtIndex:indexPath.row] objectForKey:@"data"]; 

這應該修復了代碼,並填充的tableView ... 你也能勾選

運行該檢查
NSLog(@"count %i", [_jsonResult count]); 

Data populated in table view

讓我KN如果這有幫助,快樂的編碼。 T