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 :: uitableview disable sticky header 
Swift :: vibrations in ios swift 
Swift :: Swift for Loop with Stride Function 
Swift :: swiftui lowercase 
Swift :: sum in array swift 
Swift :: cellwidget to setvalue 
Ruby :: rails get list of tables 
Ruby :: ruby file extension 
Ruby :: rails resources except 
Ruby :: rails activestorage get image url 
Ruby :: run rake task in rails console 
Ruby :: ruby format date time 
Ruby :: rspec parallel tests 
Ruby :: ruby each with index 
Ruby :: Your Ruby version is 3.0.0, but your Gemfile specified 2.7.4 
Ruby :: safe navigation operator in ruby 
Ruby :: rails validate email 
Ruby :: method delete rails not working 
Ruby :: rails activerecord to hash 
Ruby :: uper case in ruby 
Ruby :: ruby debugger 
Ruby :: capitalize composed name ruby 
Ruby :: pg_ctl: no database directory specified and environment variable PGDATA unset 
Ruby :: ruby array shift 
Ruby :: ruby generate task 
Ruby :: ruby loop using integer 
Ruby :: ide for ruby 
Ruby :: how to unloack user devise rails 
Ruby :: input type checkbox checked with condition rails 
Ruby :: rotate array by k times in rails 
ADD CONTENT
Topic
Content
Source link
Name
4+1 =