Tuesday, 15 July 2014

ios - NSJSONSerialization Json Formatting -



ios - NSJSONSerialization Json Formatting -

so have array wanna convert nsdictionary. process works great add together heading json string.

nsarray *showarray = [[mmistore defaultstore] loadallshows ]; nsmutablearray* jsonarray = [[nsmutablearray alloc] init]; (show* show in showarray) { nsmutabledictionary* showdictionary = [[nsmutabledictionary alloc] init]; [showdictionary setobject:show.showid forkey:@"showid"]; [jsonarray addobject:showdictionary]; } nsdata* nsdata = [nsjsonserialization datawithjsonobject:jsonarrayoptions:nsjsonreadingmutablecontainers error:nil]; nsstring* jsonstring =[[nsstring alloc] initwithdata:nsdata encoding:nsutf8stringencoding]; nslog(@"%@", jsonstring);

below outputs :

{[ { "showid" : "10027" }, { "showid" : "10707" }, { "showid" : "10759" }..... ]

how create this

{ "shows":[ { "showid" : "10027" }, { "showid" : "10707" }, { "showid" : "10759" }.... ] }

just add together array dictionary right key.

nsarray *showarray = [[mmistore defaultstore] loadallshows]; nsmutablearray* jsonshowsarray = [[nsmutablearray alloc] init]; (show* show in showarray) { nsmutabledictionary* showdictionary = [[nsmutabledictionary alloc] init]; [showdictionary setobject:show.showid forkey:@"showid"]; [jsonshowsarray addobject:showdictionary]; } nsdictionary* jsondictionary = [nsdictionary dictionarywithobject:jsonshowsarray forkey:@"shows"] nsdata* nsdata = [nsjsonserialization datawithjsonobject:jsondictionary options:nsjsonreadingmutablecontainers error:nil];

ios nsjsonserialization

No comments:

Post a Comment