Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SWIFT 3-从JSON web url的字符串中取出html标记_Html_Ios_Json_Swift_Swift3 - Fatal编程技术网

SWIFT 3-从JSON web url的字符串中取出html标记

SWIFT 3-从JSON web url的字符串中取出html标记,html,ios,json,swift,swift3,Html,Ios,Json,Swift,Swift3,我想知道如何将HTML标记从web url的JSON中剥离出来。我是否必须使用类似的NSString 因此,我希望去掉摘要值中的html标记。我环顾了一下abit,它说可以使用NSString,但我不确定这是否可以在Swift 3中实现。任何帮助都将不胜感激 我的代码: import UIKit import Alamofire struct postinput { let mainImage : UIImage! let name : String! let auth

我想知道如何将HTML标记从web url的JSON中剥离出来。我是否必须使用类似的
NSString

因此,我希望去掉摘要值中的html标记。我环顾了一下abit,它说可以使用NSString,但我不确定这是否可以在Swift 3中实现。任何帮助都将不胜感激

我的代码:

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


}

您可以使用此代码剥离html标记

从你之前的问题

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)
guard let dic=post[“summary”]as?[String:Any],让summary=dic[“value”]as?其他字符串{
返回
}
让str=summary.replacingOccurrences(of:“]+>”,with:”,选项:。正则表达式,范围:nil)
打印(str)
编辑

我已经检查过了,它正在工作

let summary = "<p>Latin text here</p>"
let str = summary.replacingOccurrences(of: "<[^>]+>", with: "", options: .regularExpression, range: nil)
print(str)
let summary=“此处为拉丁文
让str=summary.replacingOccurrences(of:“]+>”,with:”,选项:。正则表达式,范围:nil)
打印(str)
这里是拉丁文


您可以发布一个您试图解析的响应的示例吗?它是来自()的JSON结构,可能与@Perpatim重复,您会将扩展代码放在哪里?没有错误,html标记仍然存在。。。。我已将“guard let dic=发布[“摘要”]作为?[String:Any],让summary=dic[“value”]as?字符串else{return let str=summary.replacingOccurrences(of:“]+>”,with:”,选项:。正则表达式,范围:nil)打印(str)}你不需要把它放在别的地方condition@rob你能发布你在摘要中得到的字符串吗?“摘要”:{“值”:“这里的拉丁文本

”,“格式”:“过滤的”}@rob我已经检查并更新了答案,它对我有效。当你打印这个字符串时
print(str)
您在控制台中得到了什么?