【问题标题】:Extract items from string从字符串中提取项目
【发布时间】:2018-08-16 01:25:29
【问题描述】:

我的 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...

在这种情况下,谁能帮帮我?

【问题讨论】:

标签: swift string split


【解决方案1】:

您可以使用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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-29
    • 1970-01-01
    • 2011-07-21
    • 2019-10-15
    • 1970-01-01
    相关资源
    最近更新 更多