2014-02-05 57 views
0

嗨朋友在我的項目中的數據來自JSONserialization。其中某些數據收到html data像這樣 <p>activity for m43 by c31</p> 我需要將此數據轉換爲nsstring並顯示出來。我不知道如何轉換它,請提前幫助我。我已經通過一些解決方案,但不清楚。如何將HTML NSData轉換爲NSString?請幫助我提前致謝ios:如何將html數據轉換爲nsstring?

-(void)connectionDidFinishLoading:(NSURLConnection *)connection { 
tweet = [[NSDictionary alloc]init]; 
    tweet = [NSJSONSerialization JSONObjectWithData:webData options:kNilOptions error:nil]; 
     [array1 addObject:[item objectForKey:@"activityTitle"]];//prints string data 
     [array2 addObject:[item objectForKey:@"activityType"]];//prints string data 
     [array3 addObject:[item objectForKey:@"id"]];//prints string data 
     [array4 addObject:[item objectForKey:@"sessionId"]];//prints string data 
[array8 addObject:[item objectForKey:@"desc"]];// prints html data 
NSLog(@"%@",array2); 
     NSString *str = [[NSString alloc] initWithData:webData encoding:NSUTF8StringEncoding]; 
     NSLog(@"hiiiiiii",str); 
} 

我不知道如何將該數組8(包含html數據)轉換爲nsstring。我需要顯示它uitableviewcell。像這樣 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString * simpleTableIdentifier = @「activismCell」; CAviewCELLCell * cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier forIndexPath:indexPath];

cell.activitytitle.text = (NSString *)[array1 objectAtIndex:indexPath.row]; 
cell.idline.text = (NSString *)[array3 objectAtIndex:indexPath.row]; 
cell.descline.text = (NSString *)[array8 objectAtIndex:indexPath.row]; 
cell.issuelabel.text = (NSString *)[array9 objectAtIndex:indexPath.row]; 
cell.timelabel.text = (NSString *)[array10 objectAtIndex:indexPath.row]; 

sessid = (NSString *)[array4 objectAtIndex:indexPath.row]; 
token = (NSString *)[array6 objectAtIndex:indexPath.row]; 
apikey = (NSString *)[array7 objectAtIndex:indexPath.row]; 
NSLog(@"%@,%@",sessid,token); 
NSString *stat =(NSString *)[array2 objectAtIndex:indexPath.row]; 
NSLog(@"%@",stat); 
NSString *trimmedString = [stat stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
if([trimmedString isEqualToString:@"Two Way Video Streaming"] || [trimmedString isEqualToString:@"One Way Video Streaming"]) { 
    [cell.startvideo setHidden:FALSE]; 
    [cell.startvideo setImage:[UIImage imageNamed:@"ca_video.png"] forState:UIControlStateNormal]; 
    [cell.startvideo addTarget:self action:@selector(playAction) forControlEvents:UIControlEventTouchUpInside]; 
} 
else 
    [cell.startvideo setHidden:TRUE]; 
return cell; 

}

+0

NSJSONSerialization永遠不會創建一個NSData對象。所以,我想知道你在哪裏得到這個NSData對象。無論如何,如果你的html被嵌入到JSON中,它很可能已經被表示爲一個NSString。 – CouchDeveloper

+0

請貼上你的代碼。這將有助於找出問題。我想你是從後端請求html數據。 – Pawan

+0

我添加了我的代碼檢查它,並幫助我 – Naresh

回答

0

轉換與HTML一的NSString到使用的NSXMLParser

NSString *urlString = @"www.myurl.com"; 
NSString *agentString = @"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_6; en-us) AppleWebKit/525.27.1 (KHTML, like Gecko) Version/3.2.1 Safari/525.27.1"; 
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: 
[NSURL URLWithString:urlString]]; 
[request setValue:agentString forHTTPHeaderField:@"User-Agent"]; 
NSData *data = [ NSURLConnection sendSynchronousRequest:request returningResponse: nil error: nil ]; 
NSString *returnData = [[NSString alloc] initWithBytes: [data bytes] length:[data length] encoding: NSUTF8StringEncoding]; 

NSLog(returnData); 

去這個webthis

+1

我已經經歷了這一點,但我沒有明白它 – Naresh

+1

好的答案,幫助我:) – Maythux

2

嘗試initWithData一個純文本字符串:編碼:的NSString的方法用你的數據創建一個字符串。

例子:

NSString *str = [[NSString alloc] initWithData:someData encoding:NSUTF8StringEncoding]; 

希望它能幫助。快樂編碼:)

+0

沒有它的不工作.. json單一數據來作爲HTML休息接收爲正常字符串 – Naresh

+0

關注這個鏈接可能會幫助你,http://behindtechlines.com/2012/05/display-html-strings-plaintext-ios/ –

相關問題