2013-04-20 67 views
0

我JSON文件是这样的:如何使用循环中的NSMutableArray对于GET对象在任何索引

{ "fileContent" : [ { "created" : "2013/04/11 - 10:00", 
    "ext" : "sql", 
    "modified" : "2013/04/11 - 10:00", 
    "name" : "directorffdsgfg", 
    "size" : "1577", 
    "type" : "file" 
    }, 
    { "created" : "2013/04/11 - 12:10", 
    "ext" : "sql", 
    "modified" : "2013/04/11 - 12:10", 
    "name" : "directory02", 
    "size" : "1577", 
    "type" : "file" 
    }, 
    { "created" : "2013/04/11 - 12:10", 
    "ext" : "zip", 
    "modified" : "2013/04/11 - 12:10", 
    "name" : "jquery-mousewheel-master", 
    "size" : "5213", 
    "type" : "file" 
    } 
], 
"files" : 9, 
"folderContent" : [ { "created" : "2013/04/11 - 05:04", 
    "ext" : "sql", 
    "modified" : "2013/04/11 - 05:04", 
    "name" : "Folder 2", 
    "size" : "1577", 
    "type" : "folder" 
    }, 
    { "created" : "2013/04/15 - 09:08", 
    "ext" : "zip", 
    "modified" : "2013/04/15 - 09:08", 
    "name" : "Folder 1", 
    "size" : "11867", 
    "type" : "folder" 
    } 
], 
"folders" : 2 
} 

我想从上面的JSON只获取文件内容,并存储在对象名称值。但我不能。

这是我的代码:

ViewController.m

#import "Object.h" 
@implementation ViewController 
{ 
    NSDictionary *titles; //this variable for read json 
    NSMutableData *Data; 
    NSMutableArray *fileContent; 
    NSString *janatan; 
    NSString *number; 
    NSInteger num; 
    NSMutableArray *recipes; 
} 

@synthesize Table; 

- (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; 

    titles = [NSJSONSerialization JSONObjectWithData:Data options:kNilOptions error:nil]; 
    fileContent = [titles objectForKey:@"fileContent"]; 
    //NSLog(@"%@",fileContent); 
    number = [titles objectForKey:@"files"]; 
    num = [number integerValue]; 
    NSLog(@"num : %d",num); 
    for (int i = 0; i <= num; i++) { 
     NSLog(@"%d",i); 
     Object *obji = [Object new]; 

// this method should read many names in fileContent but not worke!!! 
     janatan = [[fileContent objectAtIndex:i]objectForKey:@"name"]; 

     NSLog(@"%@",janatan); 
     obji.nameLable = janatan; 
     if(!recipes){ 
      recipes = [NSMutableArray array]; 
     } 
     [recipes addObject:janatan]; 
     NSLog(@"object : %@",recipes); 
    } 

    [Table reloadData]; 
} 
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
    UIAlertView *errorView = [[UIAlertView alloc]initWithTitle:@"Error" message:@"The Connection has been LOST" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]; 
    [errorView show]; 
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 
} 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; 
    NSURL *url =[NSURL URLWithString:@"http://192.168.1.105/janatan/root/developers/api/filemanager.php?key=5147379269&getlist=root"]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
    [[NSURLConnection alloc]initWithRequest:request delegate:self]; 

} 

#pragma mark - Table view data source 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 1; 
} 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    return num; 
} 
- (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]; 
    } 

    return cell; 
} 
@end 
+0

您的问题有关JSON或NSMultableArray? – Jack 2013-04-20 13:00:09

+1

您添加到“食谱”数组中的'mamal'对象来自哪里?这不会出现在您的代码中的任何其他地方。除此之外,'num'似乎与你的JSON数据是'9',但你的'fileContent'数组中只有3个元素... – omz 2013-04-20 13:02:22

回答

2

更换for (int i = 0; i <= num; i++) 到:

for (NSDictionary* dict in fileContent) { 
    ....... 
    janatan = [dict objectForKey:@"name"]; 
    ....... 
} 
+0

它对你有帮助吗? – 2013-04-20 17:34:05

相关问题