解析json获取数据

阅读更多
NSError *error;
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://api.douban.com/v2/book/search?tag=computer"]];
    
    //将请求的url数据放到NSData对象中
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    
    //IOS5自带解析类NSJSONSerialization从response中解析出数据放到字典中
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
    //获取数组list的内容
    NSArray *list = [dic objectForKey:@"books"];
    //初始化成员变量
    listArrayBook = [[NSMutableArray alloc]init];

然后给成员变量添加想获取的数据就OK了。
给imageview添加Image时,若图片是存在在网址上的需要先转化一下
NSURL * url = [NSURL URLWithString:@"http:    "];
    NSData * data = [[NSData alloc]initWithContentsOfURL:url];
    UIImage *image = [[UIImage alloc]initWithData:data];

你可能感兴趣的:(json,添加image,解析json)