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

string to json swift

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

convert json to json string ios swift

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

    }
Comment

convert json to json string ios swift

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

    }
Comment

PREVIOUS NEXT
Code Example
Swift :: Return different data types swift 
Swift :: how to add button dynamically in swift 4 
Swift :: Fetch structure from userdefaults ios swift 
Swift :: get index filter swift 
Swift :: Decimal to Double conversion in Swift 
Swift :: swift remove all duplicates from an array 
Swift :: how to disable uitableview scrolling in swift 
Swift :: get class name swift 
Swift :: pop last element array swift 
Swift :: IOS create UIAlertViewController programmatically 
Swift :: and in swift6 
Swift :: swift get device screen size 
Swift :: get day difference between two dates swift 
Swift :: swift push view controller programmatically 
Swift :: swift completion handler 
Swift :: change individual element alignment swiftui 
Swift :: set white place holder color in swift 
Swift :: ios get device id 
Swift :: create button with icon swift 
Swift :: type String and int swift addition 
Swift :: swift extension Array with type 
Swift :: enum in swift 
Swift :: add arc swiftui 
Swift :: Modifying Value Types from Method Swift 
Swift :: swift disable modal dismiss swift 
Swift :: swift loop through array of objet 
Swift :: Swift break statement with nested loops 
Swift :: Swift Bitwise NOT Operator 
Swift :: uilabel without constraint 
Swift :: How to load Google map styling from json file for ios swift? 
ADD CONTENT
Topic
Content
Source link
Name
9+5 =