I have a JSON file I am trying to extract data from and I keep getting errors that I cannot resolve for some reason, any advice?
Here is how I am running it in playgrounds
import UIKit
struct GETprojects: Codable {
var projectGetResult: Bool
var projectGetProjects: ProjectGetProjects
}
struct ProjectGetProjects: Codable {
var projects: [Projects] = Array()
}
struct Projects: Codable {
var id: String
var name: String
var status: String
var category: String
var type: String
var updated: String
var value: String
var units: String
var address: Address
var contacts: Contacts
}
struct Address: Codable {
var erf: String
var street: String
var street2: String
var town: String
var city: String
var region: String
var postalCode: String
}
struct Contacts: Codable {
var professional, contractor: Contact
}
struct Contact: Codable {
var id: String
var name: String
var company: String
var phone: String
var mobile: String
var email: String
var type: String
}
'''
let JSONString = """
{
"projectGetResult": true,
"projectGetProjects": [
{
"id": "123-uni-que",
"name": "Amazon",
"status": "Planning",
"category": "Commercial",
"type": "Office Block",
"updated": "21 March 2023",
"value": 100000000,
"units": 1,
"address": {
"erf": "101-RE",
"street": "First Avenue",
"street2": "",
"town": "Cape Town",
"city": "Cape Town",
"region": "Western Cape",
"postalCode": "111"
},
"contacts": {
"professional": [
{
"id": "pr123",
"name":"Charles Darwin",
"company":"Darwin Designs",
"phone":"011 234 5678",
"mobile":"071 234 5678",
"email":"[email protected]",
"type":"Architect"
},
{
"id": "pr234",
"name":"Bill Hutchins",
"company":"Hutchins Engineering",
"phone":"012 345 6789",
"mobile":"072 345 6789",
"email":"[email protected]",
"type":"Engineer"
},
{
"id": "pr345",
"name":"Kevin Counter",
"company":"KCQS",
"phone":"013 456 7890",
"mobile":"073 456 7890",
"email":"[email protected]",
"type":"Quantity Surveyor"
}
],
"contractor": [
{
"id": "con123",
"name":"Leonardo da Vinci",
"company":"MegaBuild",
"phone":"014 567 8901",
"mobile":"074 567 8901",
"email":"[email protected]",
"type":"Main"
},
{
"id": "con234",
"name":"Steve Cleer",
"company":"SC Civils",
"phone":"015 678 9012",
"mobile":"075 678 9012",
"email":"[email protected]",
"type":"Civil"
},
{
"id": "con345",
"name":"Warren Fright",
"company":"Electrified",
"phone":"016 789 0123",
"mobile":"076 789 0123",
"email":"@sparky.com",
"type":"Electrical"
}
]
}
},
{
"id": "234-uni-que",
"name": "TBC SA",
"status": "On Site",
"category": "Commercial",
"type": "Office Block",
"updated": "21 March 2023",
"value": 200000000,
"units": 2,
"address": {
"erf": "102-RE",
"street": "Second Avenue",
"street2": "Corner of 4th",
"town": "Stellenbosch",
"city": "Cape Town",
"region": "Western Cape",
"postalCode": "222"
},
"contacts": {
"professional": [
{
"id": "pr123",
"name":"Charles Darwin",
"company":"Darwin Designs",
"phone":"011 234 5678",
"mobile":"071 234 5678",
"email":"[email protected]",
"type":"Architect"
},
{
"id": "pr234",
"name":"Bill Hutchins",
"company":"Hutchins Engineering",
"phone":"012 345 6789",
"mobile":"072 345 6789",
"email":"[email protected]",
"type":"Engineer"
},
{
"id": "pr345",
"name":"Kevin Counter",
"company":"KCQS",
"phone":"013 456 7890",
"mobile":"073 456 7890",
"email":"[email protected]",
"type":"Quantity Surveyor"
}
],
"contractor": [
{
"id": "con123",
"name":"Leonardo da Vinci",
"company":"MegaBuild",
"phone":"014 567 8901",
"mobile":"074 567 8901",
"email":"[email protected]",
"type":"Main"
},
{
"id": "con234",
"name":"Steve Cleer",
"company":"SC Civils",
"phone":"015 678 9012",
"mobile":"075 678 9012",
"email":"[email protected]",
"type":"Civil"
},
{
"id": "con345",
"name":"Warren Fright",
"company":"Electrified",
"phone":"016 789 0123",
"mobile":"076 789 0123",
"email":"@sparky.com",
"type":"Electrical"
}
]
}
},
{
"id": "345-uni-que",
"name": "Rolling Stock",
"status": "Out to Tender",
"category": "Commercial",
"type": "Office Block",
"updated": "21 March 2023",
"value": 300000000,
"units": 3,
"address": {
"erf": "103-RE",
"street": "Third Avenue",
"street2": "",
"town": "Swellendam",
"city": "Drakenstein",
"region": "Western Cape",
"postalCode": "333"
},
"contacts": {
"professional": [
{
"id": "pr123",
"name":"Charles Darwin",
"company":"Darwin Designs",
"phone":"011 234 5678",
"mobile":"071 234 5678",
"email":"[email protected]",
"type":"Architect"
},
{
"id": "pr234",
"name":"Bill Hutchins",
"company":"Hutchins Engineering",
"phone":"012 345 6789",
"mobile":"072 345 6789",
"email":"[email protected]",
"type":"Engineer"
},
{
"id": "pr345",
"name":"Kevin Counter",
"company":"KCQS",
"phone":"013 456 7890",
"mobile":"073 456 7890",
"email":"[email protected]",
"type":"Quantity Surveyor"
}
],
"contractor": [
{
"id": "con123",
"name":"Leonardo da Vinci",
"company":"MegaBuild",
"phone":"014 567 8901",
"mobile":"074 567 8901",
"email":"[email protected]",
"type":"Main"
},
{
"id": "con234",
"name":"Steve Cleer",
"company":"SC Civils",
"phone":"015 678 9012",
"mobile":"075 678 9012",
"email":"[email protected]",
"type":"Civil"
},
{
"id": "con345",
"name":"Warren Fright",
"company":"Electrified",
"phone":"016 789 0123",
"mobile":"076 789 0123",
"email":"@sparky.com",
"type":"Electrical"
}
]
}}]}
"""
print(JSONString)
let jsonData = JSONString.data(using: .utf8)!
do {
let decoder = JSONDecoder()
let tableData = try decoder.decode(GETprojects.self, from: jsonData)
print(tableData)
print("Rows in array: (tableData.projectGetProjects.count)")
}
catch {
print (error)
}`
getting the error:
**Value of type 'GETprojects' has no member 'GETprojects'**
at: print("Rows in array: (tableData.GETprojects.projectGetProjects.count)")
No matter how I configure the tableData I get a similar error, what am I missing?
2
Answers
Maybe you’ve created the wrong model.
Try this model and follow this website to create a model. https://app.quicktype.io/