SWIFT 3 - 从JSON网址获取的字符串中取出html标签 [英] SWIFT 3 - take out html tags from string taken from JSON web url

查看:224
本文介绍了SWIFT 3 - 从JSON网址获取的字符串中取出html标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道HTML标签如何从Web网址中脱离JSON。我是否需要使用类似的 NSString

所以我正在寻找去掉汇总值中的html标签。我看了看附近的东西,它说NSString可以使用,但我不知道这是否可以实施到Swift 3中。任何帮助将不胜感激。



我的代码

  import UIKit 
导入Alamofire

struct postinput {
让mainImage:UIImage!
让name:String!
让作者:字符串!
让汇总:字符串!




类TableViewController:UITableViewController {

var postsinput = [postinput]()

var mainURL =https://www.example.com/api
$ b $ typealias JSONstandard = [String:AnyObject]

覆盖func viewDidLoad(){
super.viewDidLoad()
//加载视图后通常从一个笔尖执行任何其他设置。
callAlamo(url:mainURL)
}

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

$ b中的响应b self.parseData(JSONData:response.data!)


))

}

func parseData(JSONData:Data){
do {
var readableJSON = try JSONSerialization.jsonObject(with:JSONData,options:.mutableContainers)as! JSONstandard
//打印(可读的JSON)

如果让posts = readableJSON [posts] as? [JSON标准] {
用于发布帖子{
let title = post [title] as!字符串

让author = post [author] as!字符串

后卫让dic = post [summary]为? [String:Any],让summary = dic [value]为? String else {
return
}


print(author)

if imageUrl = post [image] as?字符串{
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 {
打印(错误)
}


}

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

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

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

让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
$ b mainLabel.font = UIFont(名称:Helvetica,size:14)

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

autLabel.text = postsinput [indexPath.row] .author
$ b $ autLabel.font = UIFont(名称:Helvetica,size:12)

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

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

sumLabel.font = UIFont(名称:Helvetica,大小:12)


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

return cell!
}

重写func didReceiveMemoryWarning(){
super.didReceiveMemoryWarning()
//处理任何可以重新创建的资源。
}


}


解决方案



您可以使用这段代码剥离html标签

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

编辑 p>

我已经检查过它,它正在运作

  let summary =< ; p>拉丁文字< / p> 
let str = summary.replacingOccurrences(of:< [>] +&,with:,options:.regularExpression,range:nil)
print(str)




拉丁文字



I was wondering how can HTML tags be stripped out of JSON from a web url. Do I have to use NSString of something similar.

So I am looking to strip out the html tags that are in the summary value. I looked around abit and it says NSString can be used but I was not sure if that was something that could be implemented into Swift 3. Any Help would be appreciated.

My code:

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.
    }


}

解决方案

You can use this code for stripping html tags

From your previous question

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)

Edit

I have checked it and it is working

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

Latin text here

这篇关于SWIFT 3 - 从JSON网址获取的字符串中取出html标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆