我有这样的json文件:
{ "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 中获取文件内容中的唯一名称值并存储在对象中。但我不能。
这是我的代码:
视图控制器.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