Skip to content
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

JSON to NSData #62

Closed
magnusstubman opened this issue Oct 6, 2014 · 5 comments
Closed

JSON to NSData #62

magnusstubman opened this issue Oct 6, 2014 · 5 comments

Comments

@magnusstubman
Copy link

Given that I have a JSON object, how do I get a NSData representation, which is suitable for sending over the network, with e.g. a POST request?

@k06a
Copy link
Contributor

k06a commented Oct 6, 2014

This can be achieved with this code:

var err: NSError?
let data = NSJSONSerialization.dataWithJSONObject(json.object!, options: nil, error: &err)

@magnusstubman
Copy link
Author

@k06a, Is there any argument for not adding a wrapper for that operation to SwiftyJSON?

@k06a
Copy link
Contributor

k06a commented Oct 6, 2014

What wrapper do you mean?

@magnusstubman
Copy link
Author

e.g. let data: NSData = someJson.toData()or similare

@LukeTangPL
Copy link
Member

Use

let object = json.object
if let data = NSJSONSerialization.dataWithJSONObject(object, options: NSJSONReadingOptions..AllowFragments, error: nil) {
   //post your data to server
} else {
   //error
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants