【问题标题】:How to remove html tags from received JSON data that is shown in SwiftUI's List view?如何从接收到的 SwiftUI 列表视图中显示的 JSON 数据中删除 html 标签?
【发布时间】:2022-01-27 12:44:58
【问题描述】:

我正在尝试使用当前位于原始 JSON 文件中的干净字符串获取图书数据描述。我设法获得了诸如书名、子树和描述之类的数据。但是在获取描述时,数据是在 HTML 标记中,例如

.. 等等。下图显示了我目前获得的数据。

下面是我用来调用 JSON 图书数据并显示在 SwiftUI 列表中的代码。

import SwiftUI

@main
struct nesteddemo_newApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

import SwiftUI

struct ContentView: View {
    @State var book: Book = Book()
    
    var body: some View {
        NavigationView {
            
            // alternatively
            List {
                ForEach(book.bookContent) { bookContent in
                    Section(header: Text(bookContent.title)) {
                        OutlineGroup(bookContent.child, children: \.child) { item in
                            Text(item.title)
                        }
                    }
                }
            }
            
            ForEach(book.bookContent) { bookContent in
                VStack {
                    Text(bookContent.title)
                    List(bookContent.child, children: \.child) { item in
                        Text(item.title)
                    }
                }
            }
            
        }.navigationViewStyle(.stack)
            .onAppear {
                loadData()
            }
    }
    
    func loadData() {
        do {
            if let url = Bundle.main.url(forResource: "સાગર મંથન", withExtension: "json") {
                let data = try Data(contentsOf: url)
                book = try JSONDecoder().decode(Book.self, from: data)
            }
        } catch {
            print("error: \(error)")
        }
    }
    
    
    
    struct Book: Identifiable, Codable {
        let id = UUID()
        var bookTitle: String = ""
        var isLive: Bool = false
        var userCanCopy: Bool = false
        var bookContent: [BookContent] = []
        
        enum CodingKeys: String, CodingKey {
            case bookTitle = "book_title"
            case isLive = "is_live"
            case userCanCopy = "user_can_copy"
            case bookContent = "book_content"
        }
    }
    
    struct BookContent: Identifiable, Codable {
        let id = UUID()
        var title, type: String
        var child: [Child]
    }
    
    struct Child: Identifiable, Codable {
        let id = UUID()
        var title, type: String
        var child: [Child]?
    }
    
    struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            ContentView()
        }
    }
}

【问题讨论】:

  • 既然这是来自捆绑的文本文件,那么为什么不在文本编辑器中打开它并使用查找和替换一劳永逸地删除 html 标记?
  • @JoakimDanielson 。有很多类似格式的 JSON 书籍存储在本地,这些 JSON 书籍也将来自服务器供用户使用。所以我正在寻找将数据传递到 Wkwebview 或在 View 加载时以编程方式隐藏/删除它的方法。
  • 我明白了,上面评论中的链接看起来像是一个前进的方向

标签: html ios json swift swiftui


【解决方案1】:

func attributedString(from str: String) -> AttributedString {
    if let theData = str.data(using: .utf16) {
        do {
            let theString = try NSAttributedString(data: theData, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil)
            return AttributedString(theString)
        } catch {  print("\(error)")  }
    }
    return AttributedString(str)
}

并将 Text(item.title) 替换为 Text(attributedString(from: item.title))

学分:@workingdog

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-20
    • 2020-11-04
    • 1970-01-01
    • 2020-01-06
    相关资源
    最近更新 更多