0

Hi friends in my project am datas from JSONserialization. where a certain data is received as html data like this <p>activity for m43 by c31</p> i need to convert this data to nsstring and have display it . i dont know how to convert it please help me thanks in advance. i have gone through some solution but am not clear.How do I convert HTML NSData to an NSString? please help me thanks in advance

-(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);
}

i dont know how to convert that array8(containing html data) to nsstring. i need to display it uitableviewcell. like this - (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;

}

3
  • NSJSONSerialization never creates a NSData object. So, I'm wondering where you did get that NSData object. Anyway, if your html is embedded in JSON it is very likely already represented as a NSString. Commented Feb 5, 2014 at 12:00
  • please paste you code. that will help in finding the issue. i think you are requesting html data from back end. Commented Feb 5, 2014 at 13:14
  • i added my code check it and help me Commented Feb 5, 2014 at 13:22

2 Answers 2

2

Try initWithData:encoding: method of NSString to create a string with your data.

Example:

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

Hope it helps. Happy coding :)

Sign up to request clarification or add additional context in comments.

2 Comments

no its not working.. json single data comes as html rest receives as normal string
0

Convert a NSString with HTML into a plain text string using 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);

go to this web or this

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.