我有一个JSON文件,我试图从中提取数据,我不断得到错误,我无法解决的一些原因,任何建议?
下面是我在Playground的运作方式
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":"darwin@archi.com",
"type":"Architect"
},
{
"id": "pr234",
"name":"Bill Hutchins",
"company":"Hutchins Engineering",
"phone":"012 345 6789",
"mobile":"072 345 6789",
"email":"bill@engi.com",
"type":"Engineer"
},
{
"id": "pr345",
"name":"Kevin Counter",
"company":"KCQS",
"phone":"013 456 7890",
"mobile":"073 456 7890",
"email":"k@county.com",
"type":"Quantity Surveyor"
}
],
"contractor": [
{
"id": "con123",
"name":"Leonardo da Vinci",
"company":"MegaBuild",
"phone":"014 567 8901",
"mobile":"074 567 8901",
"email":"leo@buildy.com",
"type":"Main"
},
{
"id": "con234",
"name":"Steve Cleer",
"company":"SC Civils",
"phone":"015 678 9012",
"mobile":"075 678 9012",
"email":"sc@earthy.com",
"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":"darwin@archi.com",
"type":"Architect"
},
{
"id": "pr234",
"name":"Bill Hutchins",
"company":"Hutchins Engineering",
"phone":"012 345 6789",
"mobile":"072 345 6789",
"email":"bill@engi.com",
"type":"Engineer"
},
{
"id": "pr345",
"name":"Kevin Counter",
"company":"KCQS",
"phone":"013 456 7890",
"mobile":"073 456 7890",
"email":"k@county.com",
"type":"Quantity Surveyor"
}
],
"contractor": [
{
"id": "con123",
"name":"Leonardo da Vinci",
"company":"MegaBuild",
"phone":"014 567 8901",
"mobile":"074 567 8901",
"email":"leo@buildy.com",
"type":"Main"
},
{
"id": "con234",
"name":"Steve Cleer",
"company":"SC Civils",
"phone":"015 678 9012",
"mobile":"075 678 9012",
"email":"sc@earthy.com",
"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":"darwin@archi.com",
"type":"Architect"
},
{
"id": "pr234",
"name":"Bill Hutchins",
"company":"Hutchins Engineering",
"phone":"012 345 6789",
"mobile":"072 345 6789",
"email":"bill@engi.com",
"type":"Engineer"
},
{
"id": "pr345",
"name":"Kevin Counter",
"company":"KCQS",
"phone":"013 456 7890",
"mobile":"073 456 7890",
"email":"k@county.com",
"type":"Quantity Surveyor"
}
],
"contractor": [
{
"id": "con123",
"name":"Leonardo da Vinci",
"company":"MegaBuild",
"phone":"014 567 8901",
"mobile":"074 567 8901",
"email":"leo@buildy.com",
"type":"Main"
},
{
"id": "con234",
"name":"Steve Cleer",
"company":"SC Civils",
"phone":"015 678 9012",
"mobile":"075 678 9012",
"email":"sc@earthy.com",
"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)
}`
获取错误:
**Value of type 'GETprojects' has no member 'GETprojects'**
at: print("Rows in array: \(tableData.GETprojects.projectGetProjects.count)")
无论我如何配置tableData,我都会得到类似的错误,我错过了什么?
2条答案
按热度按时间knsnq2tg1#
也许您创建了错误的模型。请尝试此模型并按照此网站创建模型。https://app.quicktype.io/
mw3dktmi2#