I
I
Ilya2019-06-24 12:48:29
Swift
Ilya, 2019-06-24 12:48:29

How to make a structure?

Good afternoon, I started to slowly learn Swift.
Faced such problem, there is an array.

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
}
],

I get it via:
Call
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()

The structure looks like this:
Structure

struct FakturyObject: Decodable{
        var date: String?
        var dueDate: String?
        var invoiceNumber: String?
        var overdue: Int?
        var valueNoTax: Int?
        var valueToPay: Int?
        var valueWithTax: Int?
}


Mistake:
error

BAD CONNECTION
keyNotFound(CodingKeys(stringValue: "intValue", intValue: nil),
Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Index 0", intValue: 0)]

It expects the keys of the top array. How to get to the elements inside?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
doublench21, 2019-06-24
@ynizhenie

Simple example:

The code
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!)

B
briahas, 2019-06-24
@briahas

It is necessary to send the correct jason - the array must have a name. And you have not only an array instead of Dictionary, but also an unnamed array of elements inside. If you can't send the correct jason - then make your own parser

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question