【问题标题】:How to show JSON array data in UITextView [closed]如何在 UITextView 中显示 JSON 数组数据
【发布时间】:2016-05-07 08:29:13
【问题描述】:

JSON 视图

//url data

{   "status": {  
"mission_id": "6",  
"mission": " Mission:<\/strong> <\/em><\/h1>\r\n\r\n     The mission of the Greater Fort Wayne Hispanic Chamber of Commerce is to advocate, promote, and facilitate the success of Hispanic-owned businesses which will fully participate in the growth, development and education of Allen and surrounding counties. \r\n    \r\n     In addition, the Chamber strives to focus on the most effective and coordinated Hispanic business development efforts maximizing public and private sector involvement.
  }
}

我的代码

 - (void)viewDidLoad  {   
[super viewDidLoad];

NSURLRequest *req=[[NSURLRequest alloc]initWithURL:[NSURL URLWithString:@"http://edutimeapp.com/toshow/chamber-of-commerc/ws/fetch_mission.php"]];            
 response =[[NSMutableData alloc]init];
[NSURLConnection connectionWithRequest:req delegate:self];


[self.textmission setText: textarray];   //textaray NSArray object name and textmission is UITextView outlet name.     


_textmission.scrollEnabled = YES;      


 self.textmission.editable = NO;
}

连接

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data    
{    
[response appendData:data];    
NSLog(@"error receving data %@",response);    
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response    
{
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{    
NSError *error;

NSLog(@"Error in receiving data %@",error);
NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves  error:&error];
NSLog(@"response data %@",json);

NSArray* results = [json objectForKey:@"status"];
textarray = [results valueForKey:@"mission"];
}

【问题讨论】:

    标签: ios objective-c iphone json


    【解决方案1】:

    使用下面的代码,

    YourTextViewName.text = [self convertHtmlPlainText:[[json objectForKey:@"status"] valueForKey:@"mission"]];
    

    将 HTML 转换为普通字符串:

    -(NSString*)convertHtmlPlainText:(NSString*)HTMLString{
        NSData *HTMLData = [HTMLString dataUsingEncoding:NSUTF8StringEncoding];
        NSAttributedString *attrString = [[NSAttributedString alloc] initWithData:HTMLData options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} documentAttributes:NULL error:NULL];
        NSString *plainString = attrString.string;
        return plainString;
    }
    

    希望对你有帮助

    【讨论】:

    • 什么问题,textArray是什么?
    • 文本数组是一个数组名,其中存储了一个键值数据
    • ok 什么问题,html标签没有去掉啊?
    • 问题是在 textview 上不显示 JSON 数据。
    • 如果你使用数组,它没有必要我已经更新了上面的代码,检查它,它对我有用
    【解决方案2】:

    您必须首先使用我使用过的属性字符串来删除 html 内容

    NSString *htmlString = [[[ResponseNotification objectForKey:@"notifs"] objectAtIndex:indexPath.row] valueForKey:@"content"];
        NSLog(@"%@",htmlString);
    
        NSMutableAttributedString *newStr = [[NSMutableAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType } documentAttributes:nil error:nil];
        [newStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Medium" size:12.0] range:NSMakeRange(0, [newStr length])];
        [newStr addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, [newStr length])];
    

    【讨论】:

    • 没有帮助你的代码:(请建议另一个答案。
    猜你喜欢
    • 2013-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-30
    • 1970-01-01
    • 2019-08-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多