我正在使用 JSON 解析通过使用 php 从 MySql 获取数据我得到了我想要的所有数据,但是当我想打印这些数据时,我创建了一个 for 循环,但它只给了我最后一个元素..这是我的代码
在 DidViewLoad 中:
- (void)viewDidLoad
{
[超级视图DidLoad];
NSString *phpUrl = @"http://dt-works.com/eman/bookOwn.php";
NSString *dbName = @"dbName";
NSString *localHost = @"localhost";
NSString *dbUser = @"dbUser";
NSString *dbPwd = @"dbPwd";
int u_id = 1;
NSString *user_id = [NSString stringWithFormat:@"%d",u_id];
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:phpUrl]];
[request setHTTPMethod:@"POST"];
NSString *post = [[NSString alloc] initWithFormat:@"dbName=%@&localHost=%@&dbUser=%@&dbPwd=%@&user_id=%@&submit=", dbName, localHost, dbUser, dbPwd, user_id];
[request setHTTPBody:[post dataUsingEncoding:NSUTF8StringEncoding]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSArray *statuses = [parser objectWithString:json_string error:nil];
int arraySize = [statuses count];
for (NSDictionary *status in statuses)
{
bo_id2 = [status objectForKey:@"bo_id"];
bo_name2 = [status objectForKey:@"bo_name"];
bo_au_id2 = [status objectForKey:@"bo_au_id"];
bo_pub_id2 = [status objectForKey:@"bo_pub_id"];
bo_num_pages2 = [status objectForKey:@"bo_num_pages"];
bo_publish_year2 = [status objectForKey:@"bo_publish_year"];
}
NSLog(@"size of array is: %d", arraySize);
for (int i=0; i<arraySize; i++) {
NSLog(@"%d:",i);
// here I want to retrieve all array elements .. when I try
NSLog(@"Book id: %@ - book name: %@", bookId, bookName);
// this give me the last elemet only not all data
// I want to get the data here to use it later .. how ???
}
}
有什么帮助吗??