Ios 如何在NSMutableArray中为任何索引处的get对象使用循环
我有这样的json文件:Ios 如何在NSMutableArray中为任何索引处的get对象使用循环,ios,objective-c,json,cocoa-touch,for-loop,Ios,Objective C,Json,Cocoa Touch,For Loop,我有这样的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", "ex
{ "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
#导入“Object.h”
@实现视图控制器
{
NSDictionary*titles;//此变量用于读取json
NSMutableData*数据;
NSMutableArray*文件内容;
NSString*janatan;
NSString*编号;
NSInteger num;
NSMutableArray*配方;
}
@综合表;
-(void)连接:(NSURLConnection*)连接DidReceiverResponse:(NSURResponse*)响应
{
数据=[[NSMutableData alloc]init];
}
-(void)连接:(NSURLConnection*)连接未接收数据:(NSData*)数据
{
[数据附录数据:数据];
}
-(无效)连接IDFinishLoading:(NSURLConnection*)连接
{
[UIApplication sharedApplication].networkActivityIndicatorVisible=否;
titles=[NSJSONSerialization JSONObjectWithData:数据选项:针织错误:nil];
fileContent=[titles objectForKey:@“fileContent”];
//NSLog(@“%@”,文件内容);
数字=[titles objectForKey:@“files”];
num=[number integerValue];
NSLog(@“num:%d”,num);
对于(int i=0;i替换for(int i=0;i您关于JSON或NSMultableArray的问题?您添加到recipes
数组中的mamal
对象来自何处?它不会出现在您的代码中的任何其他地方。除此之外,num
似乎是9
与您的JSON数据,但您的fileContent
中只有3个元素数组。。。
for (NSDictionary* dict in fileContent) {
.......
janatan = [dict objectForKey:@"name"];
.......
}