2

How can I serialize JSON in swift? I am trying to serialize using this method, however it is causing EXC_BAD_INSTRUCTION. For downloading JSON data, I am using NSURLConnection.

var sJson : NSDictionary = NSJSONSerialization.JSONObjectWithData(nsMutData, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary

How can I solve it?

Regards

4
  • Do you mean deserialisation of the data? JSONObjectWithData will turn your NSMutableData object into an NSDictionary or NSArray, so that's a deserialisation process. Your code fragment looks valid as well - it worked for me. Commented Jun 8, 2014 at 13:04
  • Try catching the error: var error: NSError?' then pass in &error` as the last parameter to JSONObjectWithData Commented Jun 8, 2014 at 13:15
  • @Bill did that but still the same problem Commented Jun 8, 2014 at 13:33
  • @rickerbh my server returns me this data: [{"0":"Saleem Anwar","FullName":"Saleem Anwar","1":"DHQ:Hospital, ","PlaceOfCurrentPosting":"DHQ:Hospital, ","2":"Dental Surgeon","Nomenclature":"Dental Surgeon","distance":58.415098441417,"lat_dest":"34.1905283","long_dest":"78.0457054"},{"0":"Sartaj Ali Shah","FullName":"Sartaj Ali Shah","1":"DHQ:Hospital, Swabi","PlaceOfCurrentPosting":"DHQ:Hospital, Swabi","2":"Dental Surgeon","Nomenclature":"Dental Surgeon","distance":105.26937892754,"lat_dest":"34.1236046","long_dest":"78.4718982"}] Commented Jun 8, 2014 at 13:35

3 Answers 3

12

Your root-level data is an array of dictionaries -- not a dictionary; therefore replace your line with:

var sJson = NSJSONSerialization.JSONObjectWithData(nsMutData, options: .MutableContainers, error: nil) as NSArray

I tested it and it now works with your data.

Here's how I tested it after having created a "json.txt" file in my project:

var filePath  = NSBundle.mainBundle().pathForResource("json", ofType:"txt")
var nsMutData = NSData(contentsOfFile:filePath)
var sJson     = NSJSONSerialization.JSONObjectWithData(nsMutData, options: .MutableContainers, error: nil) as NSArray
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome man. I couldn't find such an obvious error. You Sir are genius
3

This is the swift 2.0 way

    let path : String = NSBundle.mainBundle().pathForResource("jsonDict", ofType: "JSON")!;
        let data : NSData = NSData(contentsOfFile: path)!;
        var jsonDictionary : NSDictionary
        do {
             jsonDictionary = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions()) as! NSDictionary
        } catch {
            print(error)
        }

Comments

0

In Swift 4+, JSON Serialization is best done by utilizing the Decodable and Encodable protocols.

There is also a detailed tutorial by Ray Wenderlich: Encoding and Decoding in Swift

1 Comment

You sure about that ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.