[英]Parse Json for UITableView iOS
This is my Json response from web service, i am having trouble to parse it. 这是Web服务的Json回复,我很难解析它。 What i want to do is load below data in tableview, for eg.
我想要做的是在表格视图中加载下面的数据,例如。 3.7 as section header and other values in rows.
3.7作为节标题和行中的其他值。 How can i do it??
我该怎么做?? i want to use built in API's no third party framework.
我想使用内置的API的无第三方框架。
Any help is greatly appreciated. 任何帮助是极大的赞赏。
{"Links":{"3.7":[{"fld_app_download_id":"7111","fld_app_version_id":"1928","fld_date_added":"2012-10-04 08:40:38","fld_cracker":"vend3tta101","fld_download_link":"fiberupload.com","fld_url":"http:\/\/fiberupload.com\/pwzmodjshwhr\/1Password_Pro_(v3.7_v370009_Univ_os31)-vend3tta101.ipa","fld_status":"1"},{"fld_app_download_id":"7112","fld_app_version_id":"1928","fld_date_added":"2012-10-04 08:40:38","fld_cracker":"vend3tta101","fld_download_link":"depositfiles.com","fld_url":"http:\/\/depositfiles.com\/files\/eyhcw06b6","fld_status":"1"},{"fld_app_download_id":"7113","fld_app_version_id":"1928","fld_date_added":"2012-10-04 08:40:38","fld_cracker":"vend3tta101","fld_download_link":"turbobit.net","fld_url":"http:\/\/turbobit.net\/vyc5jkr5s51k.html","fld_status":"1"}]}}
Have you looked at the NSJSONSerialization
class? 您是否看过
NSJSONSerialization
类? It handles conversion between JSON and property lists. 它处理JSON和属性列表之间的转换。
In a simple way,take a look at the code bellow: 以一种简单的方式,看一下下面的代码:
#import "YourViewController.h"
@interface NSDictionary(JSONCategories)
+(NSDictionary*)dictionaryWithContentsOfJSONURLString:(NSString*)urlAddress;
-(NSData*)toJSON;
@end
@implementation NSDictionary(JSONCategories)
+(NSDictionary*)dictionaryWithContentsOfJSONURLString:(NSString*)urlAddress{
NSData* data = [NSData dataWithContentsOfURL: [NSURL URLWithString: urlAddress] ];
__autoreleasing NSError* error = nil;
id result = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
if (error != nil) return nil;
return result;
}
-(NSData*)toJSON{
NSError* error = nil;
id result = [NSJSONSerialization dataWithJSONObject:self options:kNilOptions error:&error];
if (error != nil) return nil;
return result;
}
@end
@implementation YourViewController
-(IBAction)downloadData:(id)sender{
NSString *yourResponseLink = @"YOUR RESPONSE LINK";
NSURL *urlString = [NSURL URLWithString:urlWroteString];
NSData* data = [NSData dataWithContentsOfURL:urlString];
[self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:NO];
}
-(void)fetchedData:(NSData *)responseData {
NSError* error;
if (responseData == nil) {
NSLog(@"NO DATA HAS BEEN FETCHED,ERROR = %@",error);
}else{
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
NSArray *yourResponseArray = [json objectForKey:@"YOUR RESPONSE KEY"];
NSLog("YOU RESPONSE IS = %@",yourResponseArray);
}
}
@end
To display the data from the response,note that you data is being store on a NSArray,so in your cellForRowAtIndexPath ,use the following method: 要显示响应中的数据,请注意您的数据正在存储在NSArray上,因此在cellForRowAtIndexPath中 ,请使用以下方法:
-(UITableViewCell *)tableView:(UITableView *)myTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//initialize your cells normally and to display the data from your response use
NSDictionary *yourSpecificData = [yourResponseArray objectAtIndex:[indexPath row]];
//and finally display some text in the cell
cell.textLabel.text = [yourSpecificData objectForKey:@"SOME KEY FROM RESPONSE"];
}
you can use " NSJsonSerialization" in ios5 ; 您可以在ios5中使用“ NSJsonSerialization”; View Code
查看代码
-(void) parseJsonData:(NSData *)data
{
NSError *error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
if (json == nil) {
NSLog(@"json parse failed \r\n");
return;
}
NSArray *songArray = [json objectForKey:@"song"];
NSLog(@"song collection: %@\r\n",songArray);
_song = songArray;
self.songIndex = 0;
NSDictionary *song = [songArray objectAtIndex:0];
NSLog(@"song info: %@\t\n",song);
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.