swift - How to write JSON mixed mode data? -
i want write combination of dictionary , data array.
my data looks this
the function use
func writejsondata() { var error: nserror? var filename:string = "testwrite.json" var dict:nsmutabledictionary = ["3dmodel":"model_student_v3", "3ddata":"testdata file", "3dcolors":"color file name"] var myarray:[float] = [1.0,-0.13131,12.0] let dirpaths = nssearchpathfordirectoriesindomains( nssearchpathdirectory.documentdirectory, nssearchpathdomainmask.userdomainmask, true ) let docsdir = dirpaths[0] string let filepath = docsdir.stringbyappendingpathcomponent(filename) if let outputjson = nsoutputstream(tofileatpath: filepath, append: false) { outputjson.open() nsjsonserialization.writejsonobject(dict, tostream: outputjson, options: nsjsonwritingoptions(), error: &error) nsjsonserialization.writejsonobject(myarray, tostream: outputjson, options: nsjsonwritingoptions(), error: &error) outputjson.close() } }
of course json not valid there no name attach array.
> { > "3dmodel": "model_student_v3", > "3dcolors": "color file name", > "3ddata": "testdata file" }[ > 1, > -0.13131, > 12 ]
it should like
{ "3dmodel": "model_student_v3", "3dcolors": "color file name", "3ddata": "testdata file", "data": [ 1, -0.13131, 12 ] }
just use dict ["data"] = myarray , write dict.
Comments
Post a Comment