我的 uitableview 加载正常,但是,我得到了这些结果,并且在我的 uitableview 上得到了空白数据。我究竟做错了什么?谢谢你的帮助。似乎我在使用字典或数组时遇到了问题,但我很困惑在哪里更改代码。
这是我的 json 结果:
{"assistant":[{"assistant":"Joe Black"},{"assistant":"Mary White"}]}
调试结果:
2013-03-27 15:10:38.132 testJson[3833:c07] its probably a dictionary
2013-03-27 15:10:38.133 testJson[3833:c07] jsonDictionary - (null)
2013-03-27 15:10:38.134 testJson[3833:c07] string is <7b226173 73697374 616e7422
3a5b7b22 61737369 7374616e 74223a22 52696320 446f6e61 746f227d 2c7b2261 73736973
74616e74 223a2252 69632044 6f6e6174 6f227d5d 7d>
2013-03-27 15:10:38.134 testJson[3833:c07] nil
2013-03-27 15:10:38.134 testJson[3833:c07] Is mutable? 1
2013-03-27 15:10:38.135 testJson[3833:c07] Is mutable? 1
2013-03-27 15:10:38.135 testJson[3833:c07] this is your datesArray (
)
这是我的.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UITextFieldDelegate, UITableViewDelegate,
UITableViewDataSource> {
NSArray *json;
}
@property (nonatomic, retain) NSArray *json;
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@end
这是我的.m
- (void)viewDidLoad
{
[super viewDidLoad];
dispatch_async(kBgQueue, ^{
NSData* data = [NSData dataWithContentsOfURL:
kLatestKivaLoansURL];
[self performSelectorOnMainThread:@selector(fetchedData:)
withObject:data waitUntilDone:YES];
});
}
- (void)fetchedData:(NSData *)responseData {
NSError* error;
self.json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions
error:&error];
if ([json isKindOfClass:[NSArray class]]) {
NSLog(@"its an array!");
NSArray *jsonArray = (NSArray *)json;
NSLog(@"jsonArray - %@",jsonArray);
}
else {
NSLog(@"its probably a dictionary");
NSDictionary *jsonDictionary = (NSDictionary *)json;
NSLog(@"jsonDictionary - %@",jsonDictionary);
}
NSLog(@"string is %@", responseData);
if ([json isKindOfClass:[NSArray class]]) {
NSLog(@"its an array");
}
if ([json isKindOfClass:[NSDictionary class]]) {
NSLog(@"its a dictionary");
}
if ([json isKindOfClass:[NSString class]]) {
NSLog(@"its a string");
}
if ([json isKindOfClass:[NSNumber class]]) {
NSLog(@"its a number");
}
if ([json isKindOfClass:[NSNull class]]) {
NSLog(@"its a null");
}
if (json == nil){
NSLog(@"nil");
}
NSError *parseError;
NSMutableArray *listOfObjects = [NSJSONSerialization JSONObjectWithData:[@"[]"
dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers
error:&parseError];
NSLog(@"Is mutable? %hhi", [listOfObjects isKindOfClass:[NSMutableArray class]]);
listOfObjects = [NSJSONSerialization JSONObjectWithData:[@"[[],{}]"
dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers
error:&parseError];
NSLog(@"Is mutable? %hhi", [listOfObjects isKindOfClass:[NSMutableArray class]]);
NSMutableArray *datesArray = [[NSMutableArray alloc] init];
for (NSDictionary *tempDict in json){
[datesArray addObject:[tempDict objectForKey:@"assistant"]];
}
NSLog(@"this is your datesArray %@", datesArray);
}
- (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 self.json.count;
}
- (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];
}
cell.textLabel.text = [[json objectAtIndex:indexPath.row] objectForKey:@"assistant"];
return cell;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end