Convert Data To Json Swift

[Solved] Convert Data To Json Swift | Swift - Code Explorer | yomemimo.com
Question : convert data to json swift

Answered by : ahsan-lwa8tagab3go

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

Source : https://stackoverflow.com/questions/39427500/swift-3-0-data-to-json-string-any | Last Update : Thu, 20 Jan 22

Question : string to json swift

Answered by : dero-nara

 let jsonText = "{\"first_name\":\"Sergey\"}" var dictonary:NSDictionary? if let data = jsonText.data(using: String.Encoding.utf8) { do { dictonary = try JSONSerialization.jsonObject(with: data, options: []) as? [String:AnyObject] if let myDictionary = dictonary { print(" First name is: \(myDictionary["first_name"]!)") } } catch let error as NSError { print(error) } }

Source : https://www.youtube.com/watch?v=tQuFrRuEgNo | Last Update : Wed, 20 Apr 22

Question : struct to json convert in swift

Answered by : gifted-gerbil-jijipubuxs7w

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 : https://stackoverflow.com/questions/33186051/swift-convert-struct-to-json | Last Update : Wed, 26 May 21

Question : convert json to json string ios swift

Answered by : delightful-dingo-flr5uu11uolc

func jsonToString(json: AnyObject){ do { let data1 = try NSJSONSerialization.dataWithJSONObject(json, options: NSJSONWritingOptions.PrettyPrinted) // first of all convert json to the data let convertedString = String(data: data1, encoding: NSUTF8StringEncoding) // the data will be converted to the string print(convertedString) // <-- here is ur string } catch let myJSONError { print(myJSONError) } }

Source : https://stackoverflow.com/questions/36370541/how-to-convert-json-to-string-in-ios-swift | Last Update : Mon, 14 Mar 22

Answers related to convert data to json swift

Code Explorer Popular Question For Swift