如何用iPhone中的/ n和br换行解析json文件?

时间:2015-08-07 11:42:32

标签: objective-c json

我正在从使用php子生成器生成的Web服务器解析json文件。但是现在json文件在内容中有/ n和br标签,当我解析文件时,应用程序显示/ n和br标签。

继承我的代码:

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

1 个答案:

答案 0 :(得分:0)

更简单代码的示例(在格式化答案中):

// 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"];
        ...

学习语言文档可以在更简单明了的代码中获得回报 消除重复也可以减少简单的错误。