Essentially, yes. The JSON object indicated by the curly brackets { }
corresponds to the Swift Dictionary object indicated by square brackets [ ]
.
JSON arrays indicated by square brackets [ ]
likewise correspond to Swift Arrays also indicated by square brackets [ ]
.
So your initial JSON:
[
{
"action": {
"type": "block"
},
"trigger": {
"url-filter": "apple.com"
}
}
]
can be decoded into a Swift array of dictionaries:
[
[
"action": [
"type": "block"
],
"trigger": [
"url-filter": "apple.com"
]
]
]
which, when written more compactly, becomes:
[["action":["type":"block"],"trigger":["url-filter":"apple.com"]]]
Either of the three approaches shown to you in this thread will work for that. It just depends how you would prefer to work with your Friend
struct.
(And you should call it Friend
instead of Friends
since it’s a single object. Then, in your saveFriends
function, your parameter would read like friends: [Friend]
, meaning the friends
parameter is an array of Friend
objects, not an array of Friends
objects. Reads much better that way and is easier to understand. After all, code is written for humans to read and understand.)