'How to remove html tags from received JSON data that is shown in SwiftUI's List view?
I'm trying to get a book data description with a clean string which is currently in a raw JSON file. I've managed to get the data such as titles of the book, child trees, and descriptions. But while fetching the description, the data is in HTML tags such as
.. etc. The below image shows what I'm currently getting as data. 
and here is my code as below which I have used to call JSON book data and showed in SwiftUI's lists.
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()
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
