【问题标题】:How to read string between to html node如何读取到 html 节点之间的字符串
【发布时间】:2016-05-09 09:09:43
【问题描述】:
我收到来自 JSON 网络服务的响应,并且我有一个节点“名称”。
{
"name": "<style>b\n {\n font-family:'helvetica';\n line-height: 200%;\n text-align: justify;\n } \n </style><b>CORN IDLIS</b><br/>"
}
我正在尝试在 CORN IDLIS 之间获取食物名称。
所以请任何人建议我如何从该节点读取或删除所有 html 标记。
【问题讨论】:
标签:
ios
objective-c
json
nsstring
【解决方案1】:
你可以检索你的字符串
NSString *htmlStr = responseDic[@"name"];
NSArray *tempArray = [htmlStr componentsSeparatedByString:@"<b>"];
NSArray *tempArray1 = [tempArray[1] componentsSeparatedByString:@"</b>"];
NSString *yourString = [tempArray1 firstObject];
【解决方案2】:
在代码下面正常工作,htmlstring是你的html
NSAttributedString *tmp =[[NSAttributedString alloc] initWithData:[htmlstring dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]} documentAttributes:nil error:nil];