ios - Unable to create valid JSON Object? -


i have been trying create valid json objects , can't seem so. i've been looking @ create json object class in swift reference , person seemed code work. have similar. here attempt:

func parseexportdb() -> [dictionary<nsstring, anyobject>]{      var exportdict: [dictionary<nsstring, anyobject>] = []      tuple in export {         var dictobj = [             "phoneid": 123,             "loclabel": tuple[crosslocation] nsstring,             "latitude": 123,             "longitude": -123,             "time" : tuple[time] nsstring,             "heading" : tuple[heading] nsstring         ]         exportdict.append(dictobj)     }      return exportdict } 

this how call function:

var exportlist = database.parseexportdb() exportdata.sendjsontoserver(exportlist) 

and how try send json has post request server:

func sendjsontoserver(exportlist: [dictionary<nsstring, anyobject>]){      if nsjsonserialization.isvalidjsonobject(exportlist){         println("is not valid json.")     }     var request = nsmutableurlrequest(url: nsurl(string: "<server link here>")!)      var session = nsurlsession.sharedsession()     request.httpmethod = "post"      var err: nserror?     if let serializeddata = nsjsonserialization.datawithjsonobject(exportlist, options: nil, error: &err){         println("export list: \(exportlist)")         if nsjsonserialization.isvalidjsonobject(exportlist){             println("is not valid json again!")         }         else{             println("valid json!")         }         request.httpbody = serializeddata         println(request.httpbody)     }     request.addvalue("application/json", forhttpheaderfield: "content-type")     request.addvalue("application/json", forhttpheaderfield: "accept")     ... } 

the issue when call first , second if nsjsonserialization.isvalidjsonobject(exportlist) statement, both seem exportlist not valid json. have no idea why. have looked @ link above , have looked @ lots of links , have said valid json should in format of nsarray or nsdictionary`. furthermore, have looked @ data , seems looks should. appreciated. thanks!

edit: mistake on code above.. anyways if me next portion great.. trying make http post request , after perform code above, portion want send data server. here attempt:

var task = session.datataskwithrequest(request, completionhandler: {data, response, error -> void in         println("request: \(request)")          var strdata = nsstring(data: data, encoding: nsutf8stringencoding)         println("body: \(strdata)")         var err: nserror?         var json = nsjsonserialization.jsonobjectwithdata(data, options: .mutableleaves, error: &err) as! nsarray         // did jsonobjectwithdata constructor return error? if so, log error console         if(err != nil) {             println(err!.localizeddescription)             let jsonstr = nsstring(data: data, encoding: nsutf8stringencoding)             println("error not parse json: '\(jsonstr)'")         } 

however, when try perform line var json = nsjsonserialization.jsonobjectwithdata(data, options: .mutableleaves, error: &err) as! nsarray, data term returns nil , i'm not entirely sure why... insight?

your code correct little detail:

you do

if nsjsonserialization.isvalidjsonobject(exportlist){     println("is not valid json.") } 

if valid show error???

that's wrong way round.. miss not

if !nsjsonserialization.isvalidjsonobject(exportlist){     println("is not valid json.") }