如何在自定义表格视图中解析 JSON 响应 [关闭]
Posted
技术标签:
【中文标题】如何在自定义表格视图中解析 JSON 响应 [关闭]【英文标题】:how to parse the JSON response in the Custom tableview [closed] 【发布时间】:2014-06-30 04:26:29 【问题描述】:我的 JSON 结构是
"Offer_Stag1":
[
"Area":"1200م2",
"HotlineNumber1":"12345678",
"HotlineNumber2":"12345678",
"Image":"almithaq.mawaqaademo.com\/AlMithaqOfferThumbImages\/19d92aa8-fbc7-49fc-9ebe-a4bdc641216b.jpg",
"LargeImage":"almithaq.mawaqaademo.com\/AlMithaqOfferThumbImages\/81ecfc05-8431-4ce7-8ac9-5e93c24d7deb.jpg",
"SectionNo":"184","Stage":"Stage1",
"TheDetails":"موقع مميز، على مدخل المرحلة الاولى، قريبة من الخدمات\u0009","TheView":" البحرية 17 متر"
,
"Area":"1205م2 – 1201م2",
"HotlineNumber1":"12345678",
"HotlineNumber2":"12345678",
"Image":"almithaq.mawaqaademo.com\/AlMithaqOfferThumbImages\/9cdcab19-49f8-4ad8-a42c-f5e9df273320.jpg",
"LargeImage":"almithaq.mawaqaademo.com\/AlMithaqOfferThumbImages\/bca29ff9-e6f3-4554-8a8c-9f685ceb9ba6.jpg",
"SectionNo":"581-582",
"Stage":"Stage1",
"TheDetails":"أرضين متلاصقتين، سكة جانبية","TheView":"البحرية 16 متر"
]
我有自定义表格单元格想要解析它,帮我出来。
提前致谢。
【问题讨论】:
【参考方案1】:self.itemshowdetailsAr=[[NSMutableArray alloc] init];
-(void)yourmethodName
aray = [[NSArray alloc] init];
NSURL *url = [[NSURL alloc] initWithString:@"yoururlname.php"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
NSLog(@"the json resut is %@",JSON);
aray = [JSON objectForKey:@"Offer_Stag1"];
for (NSDictionary *tmp in aray)
NSMutableDictionary *itemshowdetails=[[NSMutableDictionary alloc]init];
[itemshowdetails setValue:[tmp objectForKey:@"Area"] forKey:@"Area"];
[itemshowdetails setValue:[tmp objectForKey:@"HotlineNumber1"] forKey:@"HotlineNumber1"];
[itemshowdetails setValue:[tmp objectForKey:@"HotlineNumber2"] forKey:@"HotlineNumber2"];
[itemshowdetails setValue:[tmp objectForKey:@"TheDetails"] forKey:@"TheDetails"];
[itemshowdetails setValue:[tmp objectForKey:@"TheView"] forKey:@"TheView"];
[self.itemshowdetailsAr addObject:itemshowdetails];
[self.tableView reloadData];
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON)
NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
];
[operation start];
#pragma mark - tablevie data source
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
return [self.itemshowdetailsAr 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 = [[self.itemshowdetailsAr objectAtIndex:indexPath.row ] objectForKey:@"Area"];
cell.detailTextLabel.text = [[self.itemshowdetailsAr objectAtIndex:indexPath.row ] objectForKey:@"HotlineNumber1"];
....
....
.....
return cell;
【讨论】:
@Anub.Karthik:感谢您的帖子,但建议使用 AFHTTPRequestOperationManage 而非 AFJSONRequestOperation ? 两者都属于 AFNetworking 格式,并且都执行相同的功能,如果您需要 AFHTTPRequestOperationManage 只尝试 sebastien FCT 答案,但这是您的选择,一切都用于时间优化, 好的,谢谢,tmpar 是什么?在上面的代码中 查看我更新的帖子【参考方案2】:尝试做这样的事情。这不是确切的解决方案,您必须按照自己的方式工作。
NSString *link = [NSString stringWithFormat:@"yourServiceURL"];
NSString *encdLink = [link stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url=[NSURL URLWithString:encdLink];
NSData *response = [NSData dataWithContentsOfURL:url];
NSError *error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:response options: NSJSONReadingMutableContainers error: &error];
现在您将数据保存在数组中,并使用 objectForKey 或 valueForKey 将其提取出来。您可以根据需要使用 NSDictionary
或 NSArray
。
这就是我从 JSON 中获取图像并显示在我的应用程序中的操作。首先从上面的代码中获取数组。然后像这样提取图像内容:
_demoArray = [json valueForKey:@"yourKeyField"];
现在你的 demoArray 中有值了。
希望这会有所帮助。
【讨论】:
感谢你的帖子,我正在使用 AFNetwork 框架 哦。在这种情况下,您需要尝试其他方法。编辑您的帖子并撰写有关 AFNetwork 课程的内容,否则类似的帖子可能会不断出现。【参考方案3】:假设您以类似的方式使用AFNetworking
获取您的JSON array
:
-(void)makeRequest
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.myServer.com"]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//AFNetworking asynchronous url request
dispatch_group_t group = dispatch_group_create();
dispatch_group_enter(group);
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
// You parse your JSON array
NSDictionary *fullResponse = [[NSDictionary alloc] init];
fullResponse = responseObject;
if ([[fullResponse objectForKey:@"status"] intValue] == 1)
NSDictionary *successResponse = [[NSDictionary alloc] init];
successResponse = [fullResponse objectForKey:@"success_response"];
NSArray *offer_Stag1 = [[Nsarray alloc] init];
Offer_Stag1 = [successResponse objectForKey:@"Offer_Stag1"];
allObjects = [[NSMutableArray alloc] init];
for (int i = 0; i < [offer_Stag1 count]; i++)
NSDictionnary *singleObject = [offer_Stag1 objectAtIndex:i];
[allObjects addObject:singleObject];
dispatch_group_leave(group);
failure:^(AFHTTPRequestOperation *operation, NSError *error)
NSLog(@"Request Failed: %@, %@", error, error.userInfo);
dispatch_group_leave(group);
];
[operation start];
dispatch_group_notify(group, dispatch_get_main_queue(), ^
// Here you call the desired method after all data have been downloaded, typically you should reload your tableView
[self.myCustomTableViewWithDataFromServer reloadData];
);
一些解释:
首先获取服务器 您将JSON object
解析为NSDictionnary
或NSarray
(或两者的组合)
您将AFNetworking request
封装在一个已调度的组中,以便在下载所有数据后重新加载您的UITableView
。
然后,我猜你做到了,你可以访问你存储数据的NSMutableArray
,以便在单元格中显示它们
您的NSMutableArray
是一个全局变量:
@interface MyCustomController()
NSMutableArray *allObjects;
【讨论】:
是的,感谢您的评论,我正在遵循这项技术。我想将数据解析为该标签,我为所有数据创建了 UILabel 我收到此错误:“AFHTTPRequestOperation”没有可见的@interface 声明选择器“setCompletionBlockWithSuccess”PLS 建议 假设您的数据“区域”有UILabel
称为“区域”。您应该在您的array
中找到正确的索引:在您的(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
中,您应该这样调用: NSDictionnary *toDisplay = [allObjects objectAtIndex:indexPath.row] area.text = [toDisplay objectForKey:@"Area"] ;
@A.S.K 您应该提供处理 AFNetworking
实现的代码以解决其他问题。以上是关于如何在自定义表格视图中解析 JSON 响应 [关闭]的主要内容,如果未能解决你的问题,请参考以下文章