2016-11-10 6 views
9

Web URLからHTMLタグをJSONから取り除く方法を知りました。私は同じようなもののNSStringを使用しなければならないのですか?SWIFT 3 - JSON Web URLから取得した文字列からhtmlタグを取り出す

私は要約値に含まれているHTMLタグを取り除くことを検討しています。私はabitの周りを見て、それはNSStringを使用することができますが、私はそれがスウィフト3に実装することができたかどうか確信していませんでした。

マイコード:あなたはあなたの前の質問

guard let dic = post["summary"] as? [String: Any], let summary = dic["value"] as? String else { 
    return 
} 
let str = summary.replacingOccurrences(of: "<[^>]+>", with: "", options: .regularExpression, range: nil) 
print(str) 

編集

IからHTMLタグを

を除去するために、このコードを使用することができます

import UIKit 
import Alamofire 

struct postinput { 
    let mainImage : UIImage! 
    let name : String! 
    let author : String! 
    let summary : String! 

} 


class TableViewController: UITableViewController { 

    var postsinput = [postinput]() 

    var mainURL = "https://www.example.com/api" 

    typealias JSONstandard = [String : AnyObject] 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
     callAlamo(url: mainURL) 
    } 

    func callAlamo(url : String){ 
     Alamofire.request(url).responseJSON(completionHandler: { 
      response in 

      self.parseData(JSONData: response.data!) 


     }) 

    } 

    func parseData(JSONData : Data) { 
     do { 
      var readableJSON = try JSONSerialization.jsonObject(with: JSONData, options: .mutableContainers) as! JSONstandard 
      // print(readableJSON) 

      if let posts = readableJSON["posts"] as? [JSONstandard] { 
       for post in posts { 
        let title = post["title"] as! String 

        let author = post["author"] as! String 

        guard let dic = post["summary"] as? [String: Any], let summary = dic["value"] as? String else { 
         return 
        } 


        print(author) 

        if let imageUrl = post["image"] as? String { 
         let mainImageURL = URL(string: imageUrl) 
         let mainImageData = NSData(contentsOf: mainImageURL!) 
         let mainImage = UIImage(data: mainImageData as! Data) 

         postsinput.append(postinput.init(mainImage: mainImage, name: title, author: author, summary: summary)) 
        } 
       } 
       DispatchQueue.main.async { 
        self.tableView.reloadData() 
       } 
      } 


     } 


     catch { 
      print(error) 
     } 


    } 

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return postsinput.count 
    } 

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "cell") 

     // cell?.textLabel?.text = titles[indexPath.row] 

     let mainImageView = cell?.viewWithTag(2) as! UIImageView 

     mainImageView.image = postsinput[indexPath.row].mainImage 

     //(cell?.viewWithTag(2) as! UIImageView).image = postsinput[indexPath.row].mainImage 

     let mainLabel = cell?.viewWithTag(1) as! UILabel 

     mainLabel.text = postsinput[indexPath.row].name 

     mainLabel.font = UIFont(name: "Helvetica", size:14) 

     let autLabel = cell?.viewWithTag(3) as! UILabel 

     autLabel.text = postsinput[indexPath.row].author 

     autLabel.font = UIFont(name: "Helvetica", size:12) 

     let sumLabel = cell?.viewWithTag(4) as! UILabel 

     sumLabel.text = postsinput[indexPath.row].summary 

     sumLabel.font = UIFont(name: "Helvetica", size:12) 


     //(cell?.viewWithTag(3) as! UILabel).text = postsinput[indexPath.row].author 

     return cell! 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 


} 
+0

あなたが解析しようとしている応答の例を投稿することができますか? – Frankie

+0

そのJSON構造体は(http://stackoverflow.com/questions/40526761/json-structure-with-swift-and-alamofire/40526993?noredirect=1#comment68294873_40526993) – rob

+0

[HTMLからプレーンテキストへの変換Swift](@stackoverflow.com/questions/28124119/convert-html-to-plain-text-in-swift) – Palpatim

答えて

12

ここではラテン語のテキストを、それをチェックして、それが

let summary = "<p>Latin text here</p>" 
let str = summary.replacingOccurrences(of: "<[^>]+>", with: "", options: .regularExpression, range: nil) 
print(str) 

を働いている

+0

コード内でRajatに行くのはどこですか? – rob

+0

@rob更新の回答 – Rajat

+0

はい@ラジャットthats正しい – rob

関連する問題