如何从 ios 中的 NSString 中删除附加内容

How to remove the Additional Content From the NSString in ios

在我的项目中,我将已解析的 JSON 响应保存在一个字符串中,在该字符串中有一些额外的字符串如何删除我已经尝试过的 NSString_stripeHTML 文件我只得到相同的值, 这是我的字符串,

第一个字符串-

"<p><span style="color: rgb(84, 84, 84); font-family: arial, sans-serif; font-size: small; line-height: 18.2000007629395px;">Aashirvaad offers a range of product that includes </span><span style="font-weight: bold; color: rgb(106, 106, 106); font-family: arial, sans-serif; font-size: small; line-height: 18.2000007629395px;">Aashirvaad Atta</span><span style="color: rgb(84, 84, 84); font-family: arial, sans-serif; font-size: small; line-height: 18.2000007629395px;">, Aashirvaad Select Atta, Aashirvaad Multigrain Atta, Iodised Salt,Aashirvaad Spices,</span><br></p>

这里"Aashirvaad offers a range of product that includes Aashirvaad AttaAashirvaad Select Atta, Aashirvaad Multigrain Atta, Iodised Salt,Aashirvaad Spices"

第二个字符串-<p>gram dall  is a dall</p&gt 这里"gram dall is a dall"

三弦- <p><span style="color: rgb(34, 34, 34); font-family: arial, sans-serif; font-size: 13px; line-height: 16.1200008392334px;">Channa is a genus of the Channidae family of snakehead fish. This genus contains 34 scientifically described species, but the most well known are probably the northern snakehead and the giant snakehead.</span><br></p>

这里"Channa is a genus of the Channidae family of snakehead fish. This genus contains 34 scientifically described species, but the most well known are probably the northern snakehead and the giant snakehead." 帮帮我。

已更新.. JSON 回应

{"success":true,"products":{"product":[{"product_id":"51","name":"Aashirvaad Atta","description":"<p><span style="color: rgb(84, 84, 84); font-family: arial, sans-serif; font-size: small; line-height: 18.2000007629395px;">Aashirvaad offers a range of product that includes </span><span style="font-weight: bold; color: rgb(106, 106, 106); font-family: arial, sans-serif; font-size: small; line-height: 18.2000007629395px;">Aashirvaad Atta</span><span style="color: rgb(84, 84, 84); font-family: arial, sans-serif; font-size: small; line-height: 18.2000007629395px;">, Aashirvaad Select Atta, Aashirvaad Multigrain Atta, Iodised Salt,Aashirvaad Spices,</span><br></p>","Attributes":[{"attribute_group_id":"7","name":"Quality","attribute":[{"attribute_id":"12","name":"Quality","text":"medium"}]}],"meta_title":"Aashirvaad Atta","meta_description":"Aashirvaad Atta","meta_keyword":"Aashirvaad Atta","tag":"","href":"http:\/\/devunne.com\/shoppingstore\/index.php?route=product\/product&product_id=51","model":"atta","categories":[{"product_id":"51","category_id":"78"}],"image":"catalog\/aashirvaad-atta-whole-wheat-noworry.jpg","images":[{"product_image_id":"2448","product_id":"51","image":"catalog\/AttaFlour.jpg","sort_order":"1"},{"product_image_id":"2449","product_id":"51","image":"catalog\/AttaFlour1.jpg","sort_order":"2"}],"sku":"","upc":"","ean":"","jan":"","isbn":"","mpn":"","location":"","quantity":"120","stock_status":"Out Of Stock","manufacturer_id":"11","manufacturer":"Atta","price":"Rs.165","special":"110.0000","discounts":[],"options":[{"product_option_id":"228","product_option_value":[{"product_option_value_id":"20","option_value_id":"54","name":"500gm","image":"","quantity":"16","subtract":"1","price":"60.0000","price_prefix":"-","weight":"0.00000000","weight_prefix":"+"},{"product_option_value_id":"21","option_value_id":"55","name":"1kg","image":"","quantity":"51","subtract":"1","price":"0.0000","price_prefix":"+","weight":"0.00000000","weight_prefix":"+"},{"product_option_value_id":"22","option_value_id":"56","name":"2kg","image":"","quantity":"21","subtract":"1","price":"120.0000","price_prefix":"+","weight":"0.00000000","weight_prefix":"+"}],"option_id":"14","name":"Kilograms","type":"select","value":"","required":"1"}],"reward":"0","points":"0","tax_class_id":"0","date_available":"2015-04-16","weight":"0.00000000","weight_class_id":"1","length":"0.00000000","width":"0.00000000","height":"0.00000000","length_class_id":"1","subtract":"1","rating":0,"reviews":0,"minimum":"0","sort_order":"1","status":"1","date_added":"2015-04-16 16:51:26","date_modified":"2015-05-07 04:46:53","viewed":"6"}]}}

不要删除HTML标签,只需使用以下方法修剪HTML内容:

-(NSString *)stringByStrippingHTML:(NSString*)str
{
  NSRange r;
  while ((r = [str rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
  {
    str = [str stringByReplacingCharactersInRange:r withString:@""];
  }
  return str;
}

试试这个....

NSDictionary *products=[jSONData valueForKey:@"products"];
NSDictionary *product=[products valueForKey:@"product"];
NSString *name=[product objectForKey:@"name"]; 

注意:JSONData 是您的响应数据.. NSData 类型。

您首先将 NSString(description) 转换为正确的 HTML 字符串,然后您需要使用 NSString_stripHtml 文件,

对于字符串 HTML 使用此 Link MWFeedParser

  NSString *des=jsonResponse[@"description"];
//MWFeedParser-NSString+HTML & GTMNSString+HTML
 NSString *htmlString=[des stringByDecodingHTMLEntities];
//NSString_stripHtml
 NSString *description=[htmlString stripHtml];

注意:jsonResponse 是您的回复。这对你有帮助