NSJSONSerialization空白的uitableview和null结果

时间:2013-03-27 19:52:56

标签: iphone ios json

我的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

1 个答案:

答案 0 :(得分:1)

什么是self.json?你分配它然后你正在检查它然后你在这里把它当作一个数组。

[[json objectAtIndex:indexPath.row] objectForKey:@"assistant"]

如果你想让它成为一个数组然后创建一个数组,检查返回的json是否为数组,将数组设置为json数组并在tableview中使用数组。

下面是我如何处理这个问题的一个例子,我使用自己的帮助器类,所以忽略那个部分,但注意我是如何检查它是否是一个数组然后我将它分配给我等待数据的NSArray。然后我在TableView中使用NSArray并从中加载我的数据。

-(void)downloadFinished:(id)sender {

    if ([sender isKindOfClass:[JsonHelper class]]) {

        JsonHelper *jsonHelper = (JsonHelper *)sender;

            NSData *data = [[NSData alloc] initWithData:jsonHelper.receivedData];
            NSDictionary *jsonReturn=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSLog(@"History Return:%@",jsonReturn);

            if ([jsonReturn objectForKey:@"calls"]) {

                if ([[jsonReturn objectForKey:@"calls"]isKindOfClass:[NSArray class]]) {

                    historyArray = [jsonReturn objectForKey:@"calls"];
                    NSLog(@"History Array:%@",historyArray);
                    [historyTableView reloadData];

                } else {

                   // Error stuff goes here it was not an array.
                }
            } 
        }

在我的TableView方法中,我只引用了数组

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return [historyArray count];
}

等......