gpt4 book ai didi

iphone - NSJSONSerialization 空白 uitableview 和 null 结果

转载 作者:行者123 更新时间:2023-11-29 04:02:40 25 4
gpt4 key购买 nike

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

最佳答案

什么是 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];
}

等等......

关于iphone - NSJSONSerialization 空白 uitableview 和 null 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15668168/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com