如何快速解码HTML实体? [英] How do I decode HTML entities in swift?
问题描述
我正在从网站提取JSON文件,收到的字符串之一是:
I am pulling a JSON file from a site and one of the strings received is:
The Weeknd ‘King Of The Fall’ [Video Premiere] | @TheWeeknd | #SoPhi
如何将‘
之类的字符转换为正确的字符?
How can I convert things like ‘
into the correct characters?
我已经建立了一个Xcode Playground来演示它:
I've made a Xcode Playground to demonstrate it:
import UIKit
var error: NSError?
let blogUrl: NSURL = NSURL.URLWithString("http://sophisticatedignorance.net/api/get_recent_summary/")
let jsonData = NSData(contentsOfURL: blogUrl)
let dataDictionary = NSJSONSerialization.JSONObjectWithData(jsonData, options: nil, error: &error) as NSDictionary
var a = dataDictionary["posts"] as NSArray
println(a[0]["title"])
推荐答案
没有做到这一点的直接方法,但是您可以使用NSAttributedString
魔术使该过程尽可能轻松(请注意,此方法将剥离所有以及HTML标记):
There's no straightforward way to do that, but you can use NSAttributedString
magic to make this process as painless as possible (be warned that this method will strip all HTML tags as well):
let encodedString = "The Weeknd <em>‘King Of The Fall’</em>"
// encodedString should = a[0]["title"] in your case
guard let data = htmlEncodedString.data(using: .utf8) else {
return nil
}
let options: [NSAttributedString.DocumentReadingOptionKey: Any] = [
.documentType: NSAttributedString.DocumentType.html,
.characterEncoding: String.Encoding.utf8.rawValue
]
guard let attributedString = try? NSAttributedString(data: data, options: options) else {
return nil
}
let decodedString = attributedString.string // The Weeknd ‘King Of The Fall’
请记住仅从主线程初始化NSAttributedString .它在下面使用了一些WebKit魔术,因此是必需的.
Remember to initialize NSAttributedString from main thread only. It uses some WebKit magic underneath, thus the requirement.
您可以创建自己的String
扩展程序以提高可重用性:
You can create your own String
extension to increase reusability:
extension String {
init?(htmlEncodedString: String) {
guard let data = htmlEncodedString.data(using: .utf8) else {
return nil
}
let options: [NSAttributedString.DocumentReadingOptionKey: Any] = [
.documentType: NSAttributedString.DocumentType.html,
.characterEncoding: String.Encoding.utf8.rawValue
]
guard let attributedString = try? NSAttributedString(data: data, options: options) else {
return nil
}
self.init(attributedString.string)
}
}
let encodedString = "The Weeknd <em>‘King Of The Fall’</em>"
let decodedString = String(htmlEncodedString: encodedString)
这篇关于如何快速解码HTML实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!