Objective c 基于json http请求和AFN网络的搜索栏
我有一个搜索栏,我想通过序列号搜索json http url。因此,如果我执行以下json url:Objective c 基于json http请求和AFN网络的搜索栏,objective-c,json,parsing,uisearchbar,Objective C,Json,Parsing,Uisearchbar,我有一个搜索栏,我想通过序列号搜索json http url。因此,如果我执行以下json url: http://www.ddproam.co.za/Central/Asset/AssetsWithSerial?Serial=S00000001 实际产量: [{"AssetID":1,"AssetName":"Asset 1","AssetDesc":"This is a manually inserted Asset","AssetTypeID":1,"AssetTypeDesc":"Th
http://www.ddproam.co.za/Central/Asset/AssetsWithSerial?Serial=S00000001
实际产量:
[{"AssetID":1,"AssetName":"Asset 1","AssetDesc":"This is a manually inserted Asset","AssetTypeID":1,"AssetTypeDesc":"This is a manually inserted Asset Type"}]
我试图通过一个搜索栏(比如serial s0000001)进行搜索,用json调用的AssetName填充我的表单元格
我已将json放入数组并尝试执行,但我收到一个应用程序崩溃错误,说明:
@property (strong, nonatomic) NSArray *loadedSearches;
'-[__NSArrayI objectForKeyedSubscript:]: unrecognized selector sent to instance 0x1d857700'
这是我的密码:
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
NSString *searchQuery = [NSString stringWithFormat:@"http://www.ddproam.co.za/Central/Asset/AssetsWithSerial?Serial='%@'",searchText];
searchQuery = [searchQuery stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [[NSURL alloc] initWithString:searchQuery];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
{
self.loadedSearches = JSON[@"AssetName"];
// refreshing the TableView when the block gets the response
[searchTableView reloadData];
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON)
{
NSLog(@"%@", error.localizedDescription);
}];
[operation start];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//return self.loadedSearches.count;
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.loadedSearches.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if(cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
if (self.loadedSearches[indexPath.row] == 0) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"TableCell Tapped" message:@"Yeah you tapped a table cell" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
NSLog(@"indexPath.row=%d loadedSearches.count=%d", indexPath.row, self.loadedSearches.count);
cell.textLabel.text = self.loadedSearches[indexPath.row][@"AssetName"];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Perform segue to candy detail
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"TableCell Tapped" message:@"Yeah you tapped a table cell" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
我想我在这里遇到了一个错误:
self.loadedSearches = JSON[@"AssetName"];
我认为您正在尝试通过键访问
NSArray
:JSON[@“AssetName”]
。这适用于NSDictionary
,但不适用于NSArray
。所以这里您需要知道JSON
变量是什么类型的类。如果它确实与您发布的示例数据匹配,则它必须是一个数组,包含一个字典:[{}]
。您可能希望将其转换为简单数组,以便在表视图中显示:
NSDictionary *d = [JSON lastObject]; // the dictionary contained in the JSON array
NSMutableArray *a = [NSMutableArray array]; // the array the table view will display
// enumerate on all key/value pair of the dictionary
[d enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
// create a string with key and value separated by a colon
NSString *s = [NSString stringWithFormat:@"%@: %@", key, obj];
// add this string to the array
[a addObject:s];
}];
// store the array, which now contains all the key/value pairs as strings, in loadedSearched
self.loadedSearches = a;