"struct to json convert in swift" Code Answer's

You're definitely familiar with the best coding language Swift that developers use to develop their projects and they get all their queries like "struct to json convert in swift" answered properly. Developers are finding an appropriate answer about struct to json convert in swift related to the Swift coding language. By visiting this online portal developers get answers concerning Swift codes question like struct to json convert in swift. Enter your desired code related query in the search bar and get every piece of information about Swift code related question on struct to json convert in swift. 

struct to json convert in swift

By Gifted GerbilGifted Gerbil on May 19, 2021
struct Sentence : Codable {
    let sentence : String
    let lang : String
}

let sentences = [Sentence(sentence: "Hello world", lang: "en"), 
                 Sentence(sentence: "Hallo Welt", lang: "de")]

do {
    let jsonData = try JSONEncoder().encode(sentences)
    let jsonString = String(data: jsonData, encoding: .utf8)!
    print(jsonString) // [{"sentence":"Hello world","lang":"en"},{"sentence":"Hallo Welt","lang":"de"}]
    
    // and decode it back
    let decodedSentences = try JSONDecoder().decode([Sentence].self, from: jsonData)
    print(decodedSentences)
} catch { print(error) }

Source: stackoverflow.com

Add Comment

0

All those coders who are working on the Swift based application and are stuck on struct to json convert in swift can get a collection of related answers to their query. Programmers need to enter their query on struct to json convert in swift related to Swift code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about struct to json convert in swift for the programmers working on Swift code while coding their module. Coders are also allowed to rectify already present answers of struct to json convert in swift while working on the Swift language code. Developers can add up suggestions if they deem fit any other answer relating to "struct to json convert in swift". Visit this developer's friendly online web community, CodeProZone, and get your queries like struct to json convert in swift resolved professionally and stay updated to the latest Swift updates. 

Swift answers related to "struct to json convert in swift"

View All Swift queries

Swift queries related to "struct to json convert in swift"

Browse Other Code Languages

CodeProZone