-
Notifications
You must be signed in to change notification settings - Fork 62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handling JSON where top level structure is an array not a dictionary #106
Comments
@tomasebrennan not sure if this will work, but I had a similar problem. In my case I found it was simply not (very clearly) documented, but well-supported. Construct top-level objects right from their dictionary/array representations using
|
This case is a little awkward: Marshal really is designed for dealing with JSON objects, and not JSON Arrays. One way to deal with this is to put your array inside and object, and then extract it. let object = { "users": jsonObjects }
let users: [User] = try object.value(for: "users") You can also use the handy |
I usually |
Thanks I'll stick with @jarsen solution for now. |
I'm going to go ahead and close this. Be sure to let us know if you have more questions. |
Hello,
Very interested in using Marshal however I'm not sure how best to handle JSON where the top level structure is an array rather than a dictionary.
We have some JSON that looks like this:
We would like to be able to map this directly into say an array of Users where User conforms to Unmarshaling i.e.
However the library seems to always need a key in order to create the unmarshaled object. The only way I can see to do this now is to have something like:
This does not seem as elegant as being able to create the array of users directly i.e.
let users: [User] = try json.value("users")
What is the best way of handling this kind of JSON format using Marshal?
Thanks
The text was updated successfully, but these errors were encountered: