Latest Post

Show progress of the task using SwiftUI ProgressView Flutter Vs React Native which is better? How to adjust SwiftUI text alignment

JSON Parsing from file and URL using swift

Today we will start with the parse json in swift. We can host JSON in local as well as URL format. Initially we will start with the local Json.

Step1 : Create a local .json file in swift as shown in below image.

Screenshot%2B2021 08 12%2Bat%2B7.28.36%2BPM

go to others and select the empty file

Screenshot%2B2021 08 12%2Bat%2B7.31.42%2BPM
give the extension as data.json or any json name. In the empty json paste the json data .Make sure the file name added is in the form of .json
like example.json

[

  {

    “name”: “schezwan noodles”,

    “cuisine”: “Chinese”,

    “price”: 100,

    “id”: 1,

    “imageName”: “noodles”

  },

  {

    “name”: “Pizza and pasta”,

    “cuisine”: “Italian”,

    “price”: 50,

    “id”: 2,

    “imageName”: “pizza”

  }

]

Step 2: Create a  new file of type swift and give the name as food as the file name  add the model of type struct and use the format as Codable in it as shown below

struct Food : Codable {

    var name: String?

    var cuisine: String?

    var price: Double?

    var id : Int?

    var imageName: String?

}

Step 3 : Create the Class name Parse Json in Swift to read the json from the local file the function readData is used to read the data from the local json file . Below code is used to read the file and return the data

class ParseJson {

     func readdata() -> [Food] {

        var dataArray = [Food]()

        if let bundlePath = Bundle.main.path(forResource: “food”, ofType: “json”) {

            do {

         if let jsondata = try?Data(contentsOf:  URL(fileURLWithPath: bundlePath))  {

                    if let decodejson = try? JSONDecoder().decode([Food].self, from: jsondata) {

                         dataArray = decodejson

                    }

                }

            } catch let error as NSError {

                print(“Json Error : (error))

            }

        }

        return dataArray

    }

}

Step 5:- function ReadData can be used where we needed to load data from the local file and display to the screen

Json Parsing from the URL using Swift

In the tutorial we will try to parse the Json from the url

Step 1: Create the class name as URL json parsing in swift

 func readJson() -> Employee? {

        var dataModel: Employee?

        if let url = URL(string: “http://dummy.restapiexample.com/api/v1/employees”) {

           URLSession.shared.dataTask(with: url) { data, response, error in

              if let data = data {

                do {

                    let res = try JSONDecoder().decode(Employee.self, from: data)

                    dataModel = res

                } catch let error {

                     print(error)

                  }

               }

           }.resume()

        }

        return dataModel

    }

ad the following code for json parsing in swift

Leave a Reply

Your email address will not be published.