artworkad シ artworkad シ - 2 years ago 220
Swift Question

NSDictionary to json string to json object using SwiftyJSON

I have a use case where I have an array of dictionaries and I need them as a json object:

var data = [Dictionary<String, String>]()
//append items
var bytes = NSJSONSerialization.dataWithJSONObject(data, options: NSJSONWritingOptions.allZeros, error: nil)
var jsonObj = JSON(NSString(data: bytes!, encoding: NSUTF8StringEncoding)!)


The first print statement gives me


the second


but I would expect it to return the first element in the json array. What I am doing wrong?

Answer Source

According to the docs, this should be all you need.

var data = [Dictionary<String, String>]()
//append items 
var jsonObj = JSON(data)


Are you having a problem with converting an array directly into a JSON object?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download