Skip to main content
edited title
Link
Mojtaba Hosseini
  • 124.3k
  • 45
  • 343
  • 328

Swift 4- The data couldn’t be read because it isn’t in the correct format

added 47 characters in body
Source Link
Dale K
  • 28.1k
  • 15
  • 59
  • 85

I am new in SWIFT. I develop projectdeveloping a project to learn how to parse JSON. I am trying to parse JSON to a struct. I tryam trying to do this way. So i getit using the code that comes next but I am getting the following error like Err The data couldn’t be read because it isn’t in the correct format. What i do:

Err The data couldn’t be read because it isn’t in the correct format.

What am I doing wrong? Also iI tried to use Alamofire but iI didn't find way to parse it to struct.

I am new in SWIFT. I develop project to learn how to parse JSON. I am trying to parse JSON to struct. I try to do this way. So i get error like Err The data couldn’t be read because it isn’t in the correct format. What i do wrong? Also i tried to use Alamofire but i didn't find way to parse it to struct.

I am developing a project to learn how to parse JSON. I am trying to parse JSON to a struct. I am trying to do it using the code that comes next but I am getting the following error:

Err The data couldn’t be read because it isn’t in the correct format.

What am I doing wrong? Also I tried to use Alamofire but I didn't find way to parse it to struct.

Source Link
George Heints
  • 1.5k
  • 3
  • 24
  • 41

Swift 4 The data couldn’t be read because it isn’t in the correct format

I am new in SWIFT. I develop project to learn how to parse JSON. I am trying to parse JSON to struct. I try to do this way. So i get error like Err The data couldn’t be read because it isn’t in the correct format. What i do wrong? Also i tried to use Alamofire but i didn't find way to parse it to struct.

func getData(){ let gitUrl = URL(string: "http://95.46.99.250:9095/api/v1/institution-categories") URLSession.shared.dataTask(with: gitUrl!) { (data, response , error) in let data = data print(data) do { let decoder = JSONDecoder() let gitData = try decoder.decode([Root].self, from: data!) } catch let err { print("\nErr", err.localizedDescription) } }.resume() } 

Struct

struct Root: Codable { let data: [InnerItem] } struct InnerItem:Codable { let id: Int? let image: String? let name: String? private enum CodingKeys : String, CodingKey { case id = "id", image = "image", name = "name" } } 

JSON

{ "data": [ { "id": 1, "name": "Пабы и бары", "image": "http://95.46.99.250:9095/storage/photos/[email protected]" }, { "id": 2, "name": "Кафе", "image": "http://95.46.99.250:9095/storage/photos/[email protected]" }, { "id": 3, "name": "Ночной клуб", "image": "http://95.46.99.250:9095/storage/photos/0201f7523bc2028f174710b51379e432.png" }, { "id": 4, "name": "Ресторан", "image": "http://95.46.99.250:9095/storage/photos/[email protected]" }, { "id": 5, "name": "Караоке-клуб", "image": "http://95.46.99.250:9095/storage/photos/microphone.png" }, { "id": 6, "name": "Суши-бар", "image": "http://95.46.99.250:9095/storage/photos/sushi.png" }, { "id": 7, "name": "Пиццерии", "image": "http://95.46.99.250:9095/storage/photos/pizza.png" }, { "id": 8, "name": "Кальянная", "image": "http://95.46.99.250:9095/storage/photos/c111d1e5ad6b90b61ac36836d220ebba.png" }, { "id": 9, "name": "Общая", "image": "http://95.46.99.250:9095/storage/photos/Group [email protected]" } ] }