2014-01-31 65 views
2

解析JSON數據圖像這是我的JSON響應,圖像視圖

{ 
     "AppConfig": { 
      "store_logo": "url", 
      "deal_status": "A", 
      "see_the_menu_btn": "A", 
      "store_id": "3", 
      "store_name": " Pizza Restaurant", 
      "bg_image": "www.my image.png" 
     } 
    } 

NSString *localwthr = [NSString stringWithFormat:@"my url"]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:localwthr]]; 
    [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{ 
    if (responsedata) { 
     NSDictionary *Dictionarydetails = [NSJSONSerialization 
              JSONObjectWithData:responsedata 
              options:kNilOptions 
              error:nil]; 
     NSLog(@"The return data is: %@",Dictionarydetails); 

     NSString *imgURL=[[Dictionarydetails objectForKey:@"AppConfig"]objectForKey:@"bg_image"]; 
     UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,100,100)]; 
     imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString: imgURL]]]; 
    } 
} 

我需要獲得關鍵bg_image,下載圖像URL值,並將其設置在UIImageView。我怎樣才能做到這一點?

+3

發佈您到目前爲止完成您所需要的代碼。 – rmaddy

+0

NSString * localwthr = [NSString stringWithFormat:@「my url」]; NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:localwthr]]; [[NSURLConnection alloc] initWithRequest:request delegate:self]; – user3251949

+0

好的,你發佈了代碼。怎麼了?你不告訴我們問題是什麼。 – rmaddy

回答

0

檢查出這個代碼,這是你需要或不作爲rmaddy告訴至於現在

NSString *imageName=[[jsonDict objectForKey:@"AppConfig"]objectForKey:@"bg_image"]; 
imageview.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString: imageName]]]; 
+0

它不適用於基於Url的圖像。** UIImage imageNamed **用於本地目錄。 –

+0

@KumarKl ya它的正確,但他沒有提及它的圖像網址,所以我剛剛提到的代碼來從json字典中獲取URL並在uiimageview中使用它 – Rajjjjjj

+0

我的網址是advantixcrm.com/prj/mitech /images/bg_images/blacktheme.png – user3251949

1

發表您完成代碼對於從URL加載圖像,然後

NSString *imgURL=[[jsonDict objectForKey:@"AppConfig"]objectForKey:@"bg_image"]; 
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString: imgURL]]]; 
+0

我同意你的觀點,但是你需要先使用NSJSONSerialization對象反序列化json內容 – Bruno

+0

jsonDict已經是NSJSONSerialization的一個對象。這是主要部分。 –

0

試試這個:

NSString *imgURL=[[jsonDict objectForKey:@"AppConfig"]objectForKey:@"bg_image"]; 
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,100,100)]; 
imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString: imgURL]]]; 
2

IMAGEURL是從來沒有像「www.my image.p ng「, imageUrl就像」http://www.serverName.com/directory/..../imageName.png

如果您的網址中有空格,那麼您必須將其轉換爲UTF8格式,這是一種標準的webURL格式。 so you should use,

imgURL = [imgURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 

//Use it as it shown in below code. 

NSString *imgURL=[[jsonDict objectForKey:@"AppConfig"]objectForKey:@"bg_image"]; 
    imgURL = [imgURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] 
    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString: imgURL]]]; 

乾杯!

+0

我的網址是http://advantixcrm.com/prj/mitech/images/bg_images/blacktheme.png – user3251949

+0

那麼你應該使用下面這一行將它轉換爲UTF8格式,imgURL = [imgURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] –