Answer the question
In order to leave comments, you need to log in
How to make a structure?
Good afternoon, I started to slowly learn Swift.
Faced such problem, there is an array.
[
{
"invoiceNumber": "FV/MON/X/369",
"date": "2019-06-01 00:00:00",
"dueDate": "2019-06-01 00:00:00",
"overdue": true,
"valueNoTax": 100,
"valueWithTax": 123,
"valueToPay": 123
},
{
"invoiceNumber": "FV/MON/X/368",
"date": "2019-06-01 00:00:00",
"dueDate": "2019-06-01 00:00:00",
"overdue": true,
"valueNoTax": 100,
"valueWithTax": 123,
"valueToPay": 123
},
{
"invoiceNumber": "FV/MON/X/367",
"date": "2019-06-01 00:00:00",
"dueDate": "2019-06-01 00:00:00",
"overdue": true,
"valueNoTax": 100,
"valueWithTax": 123,
"valueToPay": 123
},
{
"invoiceNumber": "FV/MON/X/366",
"date": "2019-06-01 00:00:00",
"dueDate": "2019-06-01 00:00:00",
"overdue": true,
"valueNoTax": 100,
"valueWithTax": 123,
"valueToPay": 123
},
{
"invoiceNumber": "FV/MON/X/363",
"date": "2019-05-04 00:00:00",
"dueDate": "2019-05-04 00:00:00",
"overdue": true,
"valueNoTax": 100,
"valueWithTax": 123,
"valueToPay": 123
},
{
"invoiceNumber": "FV/MON/X/362",
"date": "2019-05-04 00:00:00",
"dueDate": "2019-05-04 00:00:00",
"overdue": true,
"valueNoTax": 100,
"valueWithTax": 123,
"valueToPay": 123
},
{
"invoiceNumber": "FV/MON/X/361",
"date": "2019-05-04 00:00:00",
"dueDate": "2019-05-04 00:00:00",
"overdue": true,
"valueNoTax": 100,
"valueWithTax": 123,
"valueToPay": 123
},
{
"invoiceNumber": "FV/MON/X/360",
"date": "2019-05-04 00:00:00",
"dueDate": "2019-05-04 00:00:00",
"overdue": true,
"valueNoTax": 100,
"valueWithTax": 123,
"valueToPay": 123
}
],
let response = "ссылка"
guard let url = URL(string: response) else {return}
URLSession.shared.dataTask(with: url) { (data, respnseData, error) in
guard let jsondata = data else {return}
do {
let decoder = JSONDecoder()
let Values = try decoder.decode([FakturyObject].self, from: jsondata)
self.FLD = Values
print("CURRENT ARRAYS")
print(self.FLD)
}catch let error{
print("BAD CONNECTION" , error)
}
}.resume()
struct FakturyObject: Decodable{
var date: String?
var dueDate: String?
var invoiceNumber: String?
var overdue: Int?
var valueNoTax: Int?
var valueToPay: Int?
var valueWithTax: Int?
}
Answer the question
In order to leave comments, you need to log in
Simple example:
let json = """
[{
"invoiceNumber": "FV/MON/X/369",
"date": "2019-06-01 00:00:00",
"dueDate": "2019-06-01 00:00:00",
"overdue": true,
"valueNoTax": 100,
"valueWithTax": 123,
"valueToPay": 123
},
{
"invoiceNumber": "FV/MON/X/368",
"date": "2019-06-01 00:00:00",
"dueDate": "2019-06-01 00:00:00",
"overdue": true,
"valueNoTax": 100,
"valueWithTax": 123,
"valueToPay": 123
},
{
"invoiceNumber": "FV/MON/X/367",
"date": "2019-06-01 00:00:00",
"dueDate": "2019-06-01 00:00:00",
"overdue": true,
"valueNoTax": 100,
"valueWithTax": 123,
"valueToPay": 123
},
{
"invoiceNumber": "FV/MON/X/366",
"date": "2019-06-01 00:00:00",
"dueDate": "2019-06-01 00:00:00",
"overdue": true,
"valueNoTax": 100,
"valueWithTax": 123,
"valueToPay": 123
},
{
"invoiceNumber": "FV/MON/X/363",
"date": "2019-05-04 00:00:00",
"dueDate": "2019-05-04 00:00:00",
"overdue": true,
"valueNoTax": 100,
"valueWithTax": 123,
"valueToPay": 123
},
{
"invoiceNumber": "FV/MON/X/362",
"date": "2019-05-04 00:00:00",
"dueDate": "2019-05-04 00:00:00",
"overdue": true,
"valueNoTax": 100,
"valueWithTax": 123,
"valueToPay": 123
},
{
"invoiceNumber": "FV/MON/X/361",
"date": "2019-05-04 00:00:00",
"dueDate": "2019-05-04 00:00:00",
"overdue": true,
"valueNoTax": 100,
"valueWithTax": 123,
"valueToPay": 123
},
{
"invoiceNumber": "FV/MON/X/360",
"date": "2019-05-04 00:00:00",
"dueDate": "2019-05-04 00:00:00",
"overdue": true,
"valueNoTax": 100,
"valueWithTax": 123,
"valueToPay": 123
}]
"""
struct FakturyObject: Decodable {
let date: String
let dueDate: String
let invoiceNumber: String
let overdue: Bool // <---- ОШИБКА БЫЛА ТУТ!
let valueNoTax: Int
let valueToPay: Int
let valueWithTax: Int
}
let data = json.data(using: .utf8)
let decoder = JSONDecoder()
let parsedData = try! decoder.decode([FakturyObject].self, from: data!)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question