iphone - How to sort this JSON -
how sort json. json below output in browser.
{ event: { july: [ { createdseatmap: "yes", status: "1", completed: "1", eventpayment: "0", } ], aug: [ { createdseatmap: "yes", status: "1", completed: "1", eventpayment: "0", } ], sept: [ ], oct: [ ], nov: [ ], dec: [ { createdseatmap: "yes", status: "1", completed: "1", eventpayment: "0", stamp: "1386019800" }, { createdseatmap: "yes", status: "1", completed: "1", eventpayment: "0", stamp: "1387921620" } ], jan: [ ], feb: [ ], mar: [ ], apr: [ ], may: [ ], june: [ ] } }
below in console when fetched.
apr = ( ); aug = ( { createdseatmap: "yes", status: "1", completed: "1", eventpayment: "0", } ); dec = ( { createdseatmap: "yes", status: "1", completed: "1", eventpayment: "0", stamp: "1386019800" }, { createdseatmap: "yes", status: "1", completed: "1", eventpayment: "0", stamp: "1387921620" } ); feb = ( ); jan = ( ); july = ( { createdseatmap: "yes", status: "1", completed: "1", eventpayment: "0", } ); june = ( ); mar = ( ); may = ( ); nov = ( ); oct = ( ); sept = ( );
below code using
nsmutablearray *dictallvalues = [[nsmutablearray alloc]init]; nsmutabledictionary *getdatadict = [[nsmutabledictionary alloc]init]; getdatadict = [self.mgetdatadict valueforkey:@"event"]; nsarray *dictallkeys = [[nsarray alloc] init]; dictallkeys = [getdatadict allkeys]; nslog(@"dict :%d", [dictallkeys count]); for(int i=0; i<[getdatadict count]; i++) { nsstring *strkey = [dictallkeys objectatindex:i]; [dictallvalues addobjectsfromarray:[getdatadict valueforkey:strkey]]; if(i==0) { nsarray *cvals = [dictallvalues valueforkey:strkey]; int c = [cvals count]; nslog(@"values:%@", cvals); nslog(@"values:%@", dictallvalues); [self.marrmoneevents addobject:[nsstring stringwithformat:@"%d", c]]; self.marrmoneettype = [dictallvalues valueforkey:@"type"]; self.marrmoneettime = [dictallvalues valueforkey:@"time"]; self.marrmoneetseatmap = [dictallvalues valueforkey:@"createdseatmap"]; self.marrmoneetseatid = [dictallvalues valueforkey:@"seatmapid"]; self.marrmoneetquant = [dictallvalues valueforkey:@"quantity"]; self.marrmoneevents = [dictallvalues valueforkey:@"eventname"]; self.marrmoneetloc = [dictallvalues valueforkey:@"location"]; self.marrmoneeteid = [dictallvalues valueforkey:@"id"]; self.marrmoneetdesc = [dictallvalues valueforkey:@"description"]; self.marrmoneetdate = [dictallvalues valueforkey:@"date"]; self.marrmoneetcurr = [dictallvalues valueforkey:@"currency"]; self.marrmoneetcost = [dictallvalues valueforkey:@"cost"]; self.marrmoneetcateg = [dictallvalues valueforkey:@"category"]; self.marrmoneetimg = [dictallvalues valueforkey:@"banner"]; nslog(@"array one:%d", [self.marrmoneevents count]); } } +(nsstring *)sendrequest:(nsmutableurlrequest *)request { nshttpurlresponse *response; nserror *error; nsdata *responsedata; responsedata = [nsurlconnection sendsynchronousrequest:request returningresponse:&response error:&error]; if(responsedata && [responsedata length]) { nsstring* astr; astr = [[nsstring alloc] initwithdata:responsedata encoding:nsasciistringencoding]; return astr; } else { return @"no record found"; } } +(nsmutabledictionary *)forgotpassw:(nsdictionary *)mailid { nsstring *urlstr = [nsstring stringwithformat:@"http://myvnt.co/api2/forgotpassword/%@",[mailid valueforkey:@"mailid"]]; nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl: [nsurl urlwithstring: [urlstr stringbyaddingpercentescapesusingencoding: nsutf8stringencoding]]]; nsstring *responsestring = [mtapi sendrequest:request]; nslog(@"response:%@", responsestring); nserror *error; nsdata *jsondata = [responsestring datausingencoding:nsutf16stringencoding]; nsdictionary *results = [nsjsonserialization jsonobjectwithdata:jsondata options:0 error:&error]; nsmutabledictionary *dict = [[nsmutabledictionary alloc]initwithdictionary:results]; nslog(@"dict in api-------------%@",dict); return dict; }
how sort please guide above. in advance.
json esentially nsdictionary, not ordered collection. have consistent ordering need print manually:
nsarray sortedkeys = [json keyssortedbyvalueusingselector:@selector(someselectorwhichsortsbymonthorder)]; [sortedkeys enumerateobjectsusingblock:^(nsstring *month, nsuinteger idx, bool *stop){ nslog(@"%@ = %@", month, json[month]); }];
but having said that, order isnt changing contents of dictionary, there reason care how looks printed on console?
edit: since want ordering table view do:
dont bother sorting above, define array contains names of months (in order). in cellforrowatindexpath: current month using indexpath.row , can access month data calling:
nsstring *cellmonth = self.orderedmonthnames[indexpath.row]; nsdictionary *monthdata = self.jsondictionary[cellmonth];
Comments
Post a Comment