2014-04-06 18 views
0

我遇到了讓我的UIImage從我收到的JSon工作的問題。 下面就讓我們一起來看看我的JSON讓UIImage從JSON工作

[ 
{ 
node_title: "Fumi", 
node_uid: "11", 
users_node_name: "pae", 
nid: "7", 
Body: "<p>This is Fumi Restaurant</p> ", 
Enterprise Address: "99/9", 
Enterprise Email: "[email protected]", 
Enterprise Tel: "08111111", 
Enterprise Logo: "<img typeof="foaf:Image" src="http://localhost/drupal/sites/default/files/Fumi.jpg" width="300" height="300" alt="" />" 
}, 
{ 
node_title: "Fuji", 
node_uid: "8", 
users_node_name: "testent", 
nid: "1", 
Body: "<p>Fuji</p> ", 
Enterprise Address: "Somwhere it belong", 
Enterprise Email: "[email protected]", 
Enterprise Tel: "02-999-9999", 
Enterprise Logo: "<img typeof="foaf:Image" src="http://localhost/drupal/sites/default/files/Fuji.png" width="320" height="320" alt="" />" 
} 
] 

這裏是我的代碼:我能夠獲得所有的JSON我收到了,並告訴他們在這個視圖控制器。但是,由於JSON的內容,[self.detail valueForKey:@「Enterprise Logo」]似乎不起作用。

順便說一句,self.detail是Json對象,我通過做preparetosegue從前一個UITableViewController傳遞。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.EnterPriseName.text = [self.detail valueForKey:@"node_title"]; 
    self.EnterPriseTel.text = [self.detail valueForKey:@"users_node_name"]; 
    self.EnterPriseBody.text = [self.detail valueForKey:@"Body"]; 
    self.EnterPriseEmail.text = [self.detail valueForKey:@"nid"]; 

    [self.picLogo setImageWithURL:[NSURL URLWithString:[self.detail valueForKey:@"Enterprise Logo"]]]; 

} 

我也嘗試這種方法工作得很好,但Enterprise Logo中的內容只能包含該img的URL。我試圖儘可能動態地做事情,所以我不期待靜態的答案。有沒有辦法解決這個問題?

[self.picLogo setImageWithURL:[NSURL URLWithString:@"http://localhost/drupal/sites/default/files/Fumi.jpg"]]; 
+2

這是不合法的JSON。它不會解析。 –

+0

@HotLicks我如何使它合法呢? – user3027109

+1

讓另一端生成合法的JSON。 –

回答

0

檢索的Enterprise Logo值只提取圖像URL後,要求使用圖像NSURLConnection,NSURLSession或AFNetworking。

例如

NSString * __block imageURL = nil; 
NSArray * objects = [[self.details valueForKey:@"Enterprise Logo"] componentsSeparatedByString:@" "]; 

[objects enumerateObjectsUsingBlock:^(id object, NSUInteger index, BOOL *stop) 
{ 
    NSRange range = [object rangeOfString:@"src="]; 
    if (range.location != NSNotFound) 
    { 
     imageURL = [object substringFromIndex:range.location+range.length]; 
     *stop = YES; 
    } 
}]; 
+0

我遵循你的方法,但它不起作用我NSLog imageURL出來,它仍然爲空。 – user3027109

+1

正如@HotLicks在他的評論中提到的那樣,JSON不合法,所以它不會解析這是爲什麼你得到一個空值。 – Jonathan

+1

@HotLicks的評論 非常感謝您的幫助!我真的很感激 – user3027109

2
Enterprise Logo: "<img typeof="foaf:Image" src="http://localhost/drupal/sites/default/files/Fuji.png" width="320" height="320" alt="" />" 

的企業徽標鍵,淡水河谷是在你的JSON的HTML。

[self.picLogo setImageWithURL:[NSURL URLWithString:[self.detail valueForKey:@"Enterprise Logo"]]]; 

您正在使用的代碼是尋找該圖像的URL。不是html。

最佳做法是讓json擁有格式中的圖片url。

http://localhost/drupal/sites/default/files/Fuji.png 

而不是其中的html。從Enterprise Logoimg標籤

+0

解釋如何解析。 –