Search
 
SCRIPT & CODE EXAMPLE
 

SWIFT

convert data to json swift

do{  
let json = try JSONSerialization.jsonObject(with: data!, options: []) as? [String : Any]
}catch{ print("erroMsg") }
Comment

struct to json convert in swift

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) }
Comment

PREVIOUS NEXT
Code Example
Swift :: swift iterate over a dictionary 
Swift :: swift hex color 
Swift :: how to change the color of back button navbar xcodee 
Swift :: Delete Realm database swift 
Swift :: loop backwards swift 
Swift :: switch case in swift language 
Swift :: activity indicator swiftui 
Swift :: change placeholder color swift 
Swift :: swift screenshot 
Swift :: ios make http request 
Swift :: change ui button swift 
Swift :: swift array remove 
Swift :: how to get the path of selected PDF/doc from file manager in ios swift programmatically 
Swift :: swiftui radio button 
Swift :: swift split string into array of 2 characters 
Swift :: Swift Class and Objects 
Swift :: swift subtract dates 
Swift :: struct vs class in swift 
Swift :: clothes that you wear in Diwali 
Swift :: Type Constraints Swift 
Swift :: swiftui divider remove padding 
Swift :: swift 5 progress bar height 
Swift :: Assignment Operators Swift 
Swift :: swiftui polygon 
Swift :: swift weekday date component values 
Swift :: Swift enum Associated Values 
Swift :: how to read music library from iphone programmatically in swift 
Swift :: Struct Instances Swift 
Swift :: swift search bar 
Ruby :: ruby json parse symbolize_keys 
ADD CONTENT
Topic
Content
Source link
Name
3+9 =