从字符串中提取项目

时间:2018-08-16 01:25:30

标签: swift string split

我的JSON数据返回一个字符串,我现在无法处理该字符串。它有这样的钥匙

"description": "<img src=\"http://www.testing.com/images/flowerOne.jpg\"><p>Erie, Pa., Aug 15, 2018 / 04:59 pm (<a href=\"http://www.testing.com\" target=\"_self\">CNA</a>).- Teachers in (name of town or district) and in communities across the nation will be in the spotlight on National Teacher Day..."

但是我无法在我的应用程序中显示确切的格式。我只需要图片网址

http://www.testing.com/images/flowerOne.jpg

和实际内容

Teachers in (name of town or district) and in communities across the nation will be in the spotlight on National Teacher Day...

在这种情况下,有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

您可以使用Swift Soup来解析响应的html字符串

例如,您的json

"description": "<img src=\"http://www.testing.com/images/flowerOne.jpg\"><p>Erie, Pa., Aug 15, 2018 / 04:59 pm (<a href=\"http://www.testing.com\" target=\"_self\">CNA</a>).- Teachers in (name of town or district) and in communities across the nation will be in the spotlight on National Teacher Day..."

您必须先获取html字符串

let htmlString = json["description"]

然后,解析您的htmlString以找到图片网址

do {
    let doc: Document = try SwiftSoup.parse(htmlString)
    if let imageLink = try doc.select("img").first()?.attr("src") {
        print("Your image url is ", imageLink)
    } else {
        print("Try break the html your self")
    }
} catch {
    print(error.localizedDescription)
}

别忘了导入SwiftSoup

import SwiftSoup