如何在UITextView中显示JSON数组数据

如何在UITextView中显示JSON数组数据

本文介绍了如何在UITextView中显示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"];
}

推荐答案

使用以下代码,

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

将HTML转换为普通字符串:

convert HTML to Normal String:

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

希望它有帮助

这篇关于如何在UITextView中显示JSON数组数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 12:26