How to convert array object to JSON string . IOS 8 . Swift 1.2 -


i got array of object

    var auditactivitydaylistjson =  array<auditactivitydaymodel>()       class auditactivitydaymodel : serializable {     var daynumber : int     var daytype : int     var daydatedisplay : string     var daydate : string      override init() {         daynumber = 0         daytype = 0         daydatedisplay = ""         daydate = ""     } } 

how can convert json string

[{"daytype":1,"daynumber":1,"daydate":"2015-06-30", "daydatedisplay":""},{"daytype":1,"daynumber":2,"daydate":"2015-07-01","daydatedisplay":""}] 

thanks answer . please help.

if want use builtin functions nsjsonserialization ( https://developer.apple.com/library/ios/documentation/foundation/reference/nsjsonserialization_class/ ) need convert objects arrays, dictionaries, strings , numbers

in case should work, converting objects dictionaries before converting json string:

    let jsoncompatiblearray = auditactivitydaylistjson.map { model in         return [             "daynumber":model.daynumber,             "daytype":model.daytype,             "daydatedisplay":model.daydatedisplay,             "daydate":model.daydate         ]     }     let data = nsjsonserialization.datawithjsonobject(jsoncompatiblearray, options: nil, error: nil)     let jsonstring = nsstring(data: data!, encoding: nsutf8stringencoding) 

for more complex scenarios recommend swiftyjson ( https://github.com/swiftyjson/swiftyjson ) makes error handling , more easier.


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -