iphone - Receive same JSON response as coming from server -
this question has answer here:
- how sort json 1 answer
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]); } }
this tried
nsarray *keys = [[getdatadict allkeys] sortedarrayusingselector: @selector(compare:)]; nsmutablearray *array = [nsmutablearray arraywithcapacity: [keys count]]; (nsstring *key in keys) { [array addobject: [getdatadict objectforkey: key]]; } nslog(@"array:%@", array);
below image describing requirement of this. need response in same way coming server, please guide above. in advance.
a complicated way of sorting keys of dictionary
nscalendar *calender = [nscalendar currentcalendar]; nsdateformatter *dateformatter = [nsdateformatter new]; [dateformatter setdateformat:@"mmm"]; nsdatecomponents *components = [nsdatecomponents new]; components.month = 1; nsmutablearray *months = [@[] mutablecopy]; nsdate *date = [nsdate date]; [months addobject:[dateformatter stringfromdate:date]]; while ([months count]<12){ date = [calender datebyaddingcomponents:components todate:date options:0]; [months addobject:[dateformatter stringfromdate:date]]; } //a sample dictionary test nsdictionary *dictionary = @{@"jan": @"", @"feb": @"", @"mar": @"", @"apr": @"", @"may": @"", @"jun": @"", @"jul": @"", @"aug": @"", @"sep": @"", @"oct": @"", @"nov": @"", @"dec": @""}; nsarray *keys = [[dictionary allkeys] sortedarrayusingcomparator: ^nscomparisonresult(nsstring * key1, nsstring * key2) { nsuinteger index1 = [months indexofobject:[key1 substringtoindex:3]]; nsuinteger index2 = [months indexofobject:[key2 substringtoindex:3]]; return [@(index1) compare:@(index2)]; }]; nslog(@"keys : %@",keys);
Comments
Post a Comment