www.bejson.com json格式化网站
{
"key1":[
"a",
{
"key2":"value2"
}
]
}
//将内容实例化为字符串对象
NSString* jasonStr = @"{"key1":["a",{"key2":"value2"}]}";
//将字符串对象转为nsdictionary对象
NSDictionary* dic = [jasonStr JSONValue];
//从字典里读取出来的value1是一个数组
NSArray* array = [dic objectForKey:@"key1"];
//从数组中取出对应的dic对象
NSDictionary* dic2 = [array objectAtIndex:1];
NSLog(@"%@", [dic2 objectForKey:@"key2"];
//-----------------------------------------------------------------------------------------------------------
同步下载
//将本地的文件读取出来后 实例化nsstring对象
NSString* jsonStr = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"douban" ofType:@"txt"] encoding:NSUTF8StringEncoding error:nil];
//加打印判断一下是否读取成功
NSLog(@"%@", jsonStr);
NSDictionary* dic = [jsonStr JSONValue];
NSArray* entryArray = [dic objectForKey:@"entry"];
NSDictionary* book1 = [entryArray objectAtIndex:0];
NSArray* linkArray = [book1 objectForKey:@"link"];
NSDictionary* urldic = [linkArray objectAtIndex:2];
NSString* urlStr = [urldic objectForKey:@"@href"];
NSURL* imageUrl = [NSURL URLWithString:@"http://pic1.desk.chinaz.com/file/11.07.10/8/ershiyiyuezmbz47.jpg" 或<@"urlStr">];
//实例化请求对象
NSURLRequest* request = [NSURLRequest requestWithURL:imageUrl];
//nsdata二进制
NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
UIImage* image = [UIImage imageWithData:data];
UIImageView* imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 150, 150)];
imageView.image = image;
[self.view addSubview:imageView];
[imageView release];
//-------------------------------------------------------------------------------------------------------------------------------------------
异步下载
@interface ViewController : UIViewController<NSURLConnectionDataDelegate>{
UIImageView* imageView;
NSMutableData* imageData;
float currentPro;
float length;
UIProgressView* proView;
//---------------------------------------
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 250)];
[self.view addSubview:imageView];
[imageView release];
UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(100, 260, 100, 40);
[button setTitle:@"看见我吧" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
imageData = [[NSMutableData alloc] init];
}
- (void)buttonClick{
NSString* urlStr = @"http://pic4.bbzhi.com/fengjingbizhi/2560x1600gaoqingfengjingbizhi/2560x1600gaoqingfengjingbizhi_350068_13.jpg";
NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]];
[NSURLConnection connectionWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
NSLog(@"开始下载");
NSHTTPURLResponse* http = (NSHTTPURLResponse*)response;
NSDictionary* dic = http.allHeaderFields;
NSString* str = [dic objectForKey:@"Content-Length"];
length = [str floatValue];
proView = [[UIProgressView alloc] initWithFrame:CGRectMake(50, 400, 220, 50)];
//刚开始为0,progress为1时就到头了
proView.progress = currentPro / length;
[self.view addSubview:proView];
[proView release];
}
//时时调用次函数
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[imageData appendData:data];
proView.progress = (float)imageData.length / length;
}
//数据下载完成后调用次函数
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
imageView.image = [UIImage imageWithData:imageData];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
NSLog(@"error");
}
- (void)dealloc{
[imageData release];
[super dealloc];
}