-2

I am currently parsing a json file from a web server which was generated using a php son generator.But now the json file has /n and br tags in the content and when i parse the file,the app displays the /n and br tags.

Heres my code:

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
    static NSString *cellIdentifier=@"identity";
    CustomCell2 *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    cell.loader.hidden = NO;
    [loader startAnimating];
    [[NSBundle mainBundle]loadNibNamed:@"CustomCell11" owner:self options:nil];
    cell=(CustomCell2 *)tblCell;
    NSMutableArray *dictionaryObject=[_newsArray objectAtIndex:indexPath.row];
    lblMain.text=[dictionaryObject valueForKey:@"MainHeadline"];
    lblSummary.text=[dictionaryObject valueForKey:@"Story"];
    lblEdition.text=[dictionaryObject valueForKey:@"Edition"];
    //lblStory.text=[dictionaryObject valueForKey:@"FullStory"];
    lblDate.text=[dictionaryObject valueForKey:@"PublishDate"];
    lblAuthor.text=[dictionaryObject valueForKey:@"Author"];




   dispatch_queue_t queue= dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
    dispatch_async(queue, ^{
    NSURL* url = [NSURL URLWithString:[dictionaryObject valueForKey:@"ImagePath"]];
    NSData *data = [NSData dataWithContentsOfURL:url];

        dispatch_sync(dispatch_get_main_queue(), ^{

                       UIImageView *imgViewThumb=[[UIImageView alloc]initWithFrame:imgView.frame];
            [imgViewThumb setImage:[UIImage imageWithData:data]];

            dispatch_async(dispatch_get_main_queue(), ^{
                //cell image added below dispath..patching,il change this later..brrrrrr!!
                [cell addSubview:imgViewThumb];
                //cell.ImagePath.UIImage = imgViewThumb;
            });

           // [cell addSubview:imgViewThumb];
        });

    });
    //what the heck???
    return cell;

}
#pragma mark -methods
-(void)retrieveData{

    NSURL * url =[NSURL URLWithString:getDataURL];
    NSData *data =[NSData dataWithContentsOfURL:url];
       _json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
    _newsArray = [[NSMutableArray alloc]init];

    for (int i = 0; i < _json.count; i++){
        {

            NSString * cID =[[_json objectAtIndex:i]objectForKey:@"ID"];
            NSString * cName = [[_json objectAtIndex:i]objectForKey:@"MainHeadline"];
            NSString * cState =[[_json objectAtIndex:i]objectForKey:@"FullStory"];
            NSString * cPopulation =[[_json objectAtIndex:i]objectForKey:@"Edition"];
            NSString * cCountry =[[_json objectAtIndex:i]objectForKey:@"PublishDate"];
            NSString * cStory =[[_json objectAtIndex:i]objectForKey:@"Story"];
            NSString * cAuthor =[[_json objectAtIndex:i]objectForKey:@"Author"];
            UIImage  *cImage =[[_json objectAtIndex:i]objectForKey:@"ImagePath"];
            UIImage  *cLogo =[[_json objectAtIndex:i]objectForKey:@"Logo"];
            CustomCell2 *myCity = [[CustomCell2 alloc]initWithCityID:cID andCityName:cName andCityState:cState andCityPopulation:cPopulation andCityCountry:cCountry andStory:cStory andImagePath:cImage andAuthor:cAuthor andLogo:cLogo];
            [_newsArray addObject:myCity];``
        }
        //[self.tblView reloadData];
    }    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [self performSegueWithIdentifier:@"detailView" sender:self];
}
#pragma mark - SlideNavigationController Methods -

- (BOOL)slideNavigationControllerShouldDisplayLeftMenu
{
    return YES;
}
  • try this one http://stackoverflow.com/questions/11786075/ios-utf8-encoding-from-nsstring – Arun Aug 07 '15 at 11:57
  • 1
    There should be no problem with lie breaks, from the JSON spec: "Whitespace can be inserted between any pair of tokens. Excepting a few encoding details, that completely describes the language." There should nt be an y html other than inside strings. Provide a sample of the JSON that has "br line breaks", that is invalid JSON. – zaph Aug 07 '15 at 11:58
  • Note that the question title like has an incorrect representation of a newline character: "/n", it should be "\n". – zaph Aug 07 '15 at 11:59
  • 1
    What you're saying is that the text strings being sent from the other end contain data you do not want to display. The answer obviously is to either get the other end to send the desired data, or to remove the extraneous data on your end. What is your problem? – Hot Licks Aug 07 '15 at 12:21
  • this is the url to the json file that has br tags:http://app.nmh.com.na/generator/getStories.php?CategoryID=1 – BrainyMonkey Aug 09 '15 at 18:15
  • The problem Hot Licks is that,when the data is parsed,there are br and n break tags inbetween the content..Could there be something wrong with the json file. – BrainyMonkey Aug 09 '15 at 18:20

1 Answers1

0

An example of better simpler code (in an answer for formatting):

// Current
for (int i = 0; i < _json.count; i++){
    {
        NSString * cID =[[_json objectAtIndex:i]objectForKey:@"ID"];
        NSString * cName = [[_json objectAtIndex:i]objectForKey:@"MainHeadline"];
        NSString * cState =[[_json objectAtIndex:i]objectForKey:@"FullStory"];
        ...

// Simplified
for (NSDictionary *item in _json) {
        NSString * cID = item[@"ID"];
        NSString * cName = item[@"MainHeadline"];
        NSString * cState =item[@"FullStory"];
        ...

Studying the language documentation can payoff in simpler and clear code.
Also elimination of duplication can reduce simple errors.

zaph
  • 111,848
  • 21
  • 189
  • 228
  • I agree,but the main problem i currently face is that the app content has br and /n tags in between the parsed content.The issue is how to support these html tags and interpret them as line breaks instead of plain text – BrainyMonkey Aug 09 '15 at 18:18