ikajava
ikajava

Reputation: 227

JSON array of objects in vapor

Maybe it's a noob question, but I can't find any solution anywhere else, so I want to ask.

I'm trying to generate JSON formatted data with Vapor in swift.

I have a class, named Customer and a class, named Parcel In Customer class, I have a variable var parcelArray = [Parcel] I added a parcels in that array in 'drop.get' Now I want to generate JSON

 return try Node(node:[ 
        "firstName" : self.firstName,
        "lastName" : self.lastName,
        "personID" : self.personID
    ])

How can I add parcelArray here? I want result like this:

{"name": "Name",
    "surname": "Surname",
    "person_id": 123123123,
    "parcel": [
       "parcelName": "parcel 1"
    ],
    "parcel": [
       "parcelName" : "parcel 2"
    ]
}

Upvotes: 1

Views: 1198

Answers (1)

Logan
Logan

Reputation: 53112

The array stuff can be a little bit tricky because at this time, generic extensions can't then conform to a protocol. In Swift 4, they will function interchangeably, in the meantime, we have a little extra work to do.

 return try Node(node:[ 
        "firstName" : self.firstName,
        "lastName" : self.lastName,
        "personID" : self.personID,
        "parcels": self.parcels.makeNode()
    ])

Let me know if that's not clear and we can be more specific re: JSON

Upvotes: 3

Related Questions