ios - Invalid top-level type in JSON write in Swift -


i'm trying put using afnetworking. need send array of participants in competition, i'm getting "invalid top-level type in json write in swift" error. json generate perfect, , if try using rest client works. here's code:

func sendusers(oncomplete: (nserror?) -> void) {     var error: nserror?     var jsondata: nsdata = nsjsonserialization.datawithjsonobject(self.createdictionaryofparticipations(), options: nsjsonwritingoptions.allzeros, error: &error)!     var jsonstring: string = nsstring(data: jsondata, encoding: nsutf8stringencoding)!     println("json: \(jsonstring)")     if self.requestmanager == nil {         self.requestmanager = afhttprequestoperationmanager();     }     self.requestmanager?.responseserializer = afjsonresponseserializer();     let requestserializer = afjsonrequestserializer()     self.requestmanager?.requestserializer = requestserializer;     self.requestmanager?.requestserializer.setvalue("application/json", forhttpheaderfield: "content-type");     self.requestmanager?.put(kparticipantsurl, parameters: jsonstring, success: { (operation: afhttprequestoperation!, object: anyobject!) -> void in         self.persistusers([])         oncomplete(nil)         }, failure: { (operation: afhttprequestoperation!, error: nserror!) -> void in             oncomplete(error)     }) } 

and method within create jsonobject:

func createdictionaryofparticipations() -> nsarray {     var dict: nsmutabledictionary = nsmutabledictionary()     var participants: nsmutablearray = nsmutablearray()     var savedparticipants: [user] = self.getusers() [user]     participant: user in savedparticipants {         var participantdict: nsmutabledictionary = nsmutabledictionary()         participantdict.setvalue(participant.name, forkey: knamekey)         participantdict.setvalue(participant.firstsurname, forkey: kfirstsurnamekey)         participantdict.setvalue(participant.secondsurname, forkey: ksecondsurnamekey)         participantdict.setvalue(participant.dni, forkey: kdnikey)         participantdict.setvalue(participant.address, forkey: kaddresskey)         participantdict.setvalue(participant.postalcode, forkey: kpostalcodekey)         participantdict.setvalue(participant.city, forkey: kcitykey)         participantdict.setvalue(participant.province, forkey: kprovincekey)         participantdict.setvalue(participant.phone, forkey: kphonekey)         participantdict.setvalue(participant.email, forkey: kemailkey)         participantdict.setvalue(participant.code, forkey: kcodekey)         participantdict.setvalue(participant.prize, forkey: kprizekey)         participants.addobject(participantdict)     }     return participants } 

if run app , send request, error:

*** terminating app due uncaught exception 'nsinvalidargumentexception', reason: '*** +[nsjsonserialization datawithjsonobject:options:error:]: invalid top-level type in json write' *** first throw call stack: ( 0   corefoundation                      0x0000000103b5df35 __exceptionpreprocess + 165 1   libobjc.a.dylib                     0x00000001037f6bb7 objc_exception_throw + 45 2   corefoundation                      0x0000000103b5de6d +[nsexception raise:format:] + 205 3   foundation                          0x000000010349b0ea +[nsjsonserialization datawithjsonobject:options:error:] + 264 4   mlb                                 0x00000001031cb23c -[afjsonrequestserializer requestbyserializingrequest:withparameters:error:] + 1164 5   mlb                                 0x00000001031c13bd -[afhttprequestserializer requestwithmethod:urlstring:parameters:error:] + 1869 6   mlb                                 0x00000001031e6d14 -[afhttprequestoperationmanager put:parameters:success:failure:] + 388 7   mlb                                 0x000000010317acf0 _tfc3mlb12usersmanager9sendusersfs0_ffgsqcso7nserror_t_t_ + 8656 8   mlb                                 0x0000000103166426 _tfc3mlb22mainmenuviewcontroller9alertviewfs0_ftcso11uialertview20clickedbuttonatindexsi_t_ + 630 9   mlb                                 0x0000000103166602 _ttofc3mlb22mainmenuviewcontroller9alertviewfs0_ftcso11uialertview20clickedbuttonatindexsi_t_ + 66 10  uikit                               0x00000001046773e0 -[uialertview _preparetodismissfortappedindex:] + 161 11  uikit                               0x0000000104676ede __35-[uialertview _preparealertactions]_block_invoke50 + 43 12  uikit                               0x0000000104670464 -[uialertcontroller _dismissanimated:triggeringaction:triggeredbypopoverdimmingview:] + 89 13  uikit                               0x00000001047c6540 _uigesturerecognizerupdate + 9487 14  uikit                               0x000000010445eff6 -[uiwindow _sendgesturesforevent:] + 1041 15  uikit                               0x000000010445fc23 -[uiwindow sendevent:] + 667 16  uikit                               0x000000010442c9b1 -[uiapplication sendevent:] + 246 17  uikit                               0x0000000104439a7d _uiapplicationhandleeventfromqueueevent + 17370 18  uikit                               0x0000000104415103 _uiapplicationhandleeventqueue + 1961 19  corefoundation                      0x0000000103a93551 __cfrunloop_is_calling_out_to_a_source0_perform_function__ + 17 20  corefoundation                      0x0000000103a8941d __cfrunloopdosources0 + 269 21  corefoundation                      0x0000000103a88a54 __cfrunlooprun + 868 22  corefoundation                      0x0000000103a88486 cfrunlooprunspecific + 470 23  graphicsservices                    0x00000001084ca9f0 gseventrunmodal + 161 24  uikit                               0x0000000104418420 uiapplicationmain + 1282 25  mlb                                 0x000000010319f84e top_level_code + 78 26  mlb                                 0x000000010319f88a main + 42 27  libdyld.dylib                       0x000000010679c145 start + 1 ) libc++abi.dylib: terminating uncaught exception of type nsexception 

i don't know where's problem... object properties put json nsobject , fields put json strings (string type, not nsstring) , i've tried used nsstring still error. ideas?? thx

you're doing json serialisation yourself, , asking afn again, you're passing nsstring , asking serialised json, won't work.

what should doing passing nsarray returned createdictionaryofparticipations instead:

self.requestmanager?.put(kparticipantsurl, parameters: self.createdictionaryofparticipations(), success:... 

Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -