iphone - Posting http JSON request using NSJSONSerializer iOS -
here code:
in .m
nsarray *keys = [nsarray arraywithobjects:@"training_code", @"training_duration",@"training_startdate",@"training_enddate",@"trainer_id",@"training_location",@"comments",@"keyword",@"numberofdays", nil]; nsarray *objects = [nsarray arraywithobjects:@"training_code", @"training_duration",@"training_startdate",@"training_enddate",@"trainer_id",@"training_location",@"comments",@"keyword",@"numberofdays", nil]; nsdata *jsondata; nsstring *jsonstring; nsdictionary *jsondictionary = [nsdictionary dictionarywithobjects:objects forkeys:keys]; if([nsjsonserialization isvalidjsonobject:jsondictionary]) { jsondata = [nsjsonserialization datawithjsonobject:jsondictionary options:0 error:nil]; jsonstring = [[nsstring alloc]initwithdata:jsondata encoding:nsutf8stringencoding]; } // sure escape url string. nsurl *url1 = [nsurl urlwithstring:@"http://xx.xx.xx.xxx/deployioscalender/service1.asmx"]; nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:url1]; [request sethttpmethod:@"post"]; [request sethttpbody: jsondata]; [request setvalue:@"application/json" forhttpheaderfield:@"content-type"]; [request setvalue:[nsstring stringwithformat:@"%d", [jsondata length]] forhttpheaderfield:@"content-length"]; nserror *errorreturned = nil; nsurlresponse *theresponse =[[nsurlresponse alloc]init]; nsdata *data = [nsurlconnection sendsynchronousrequest:request returningresponse:&theresponse error:&errorreturned]; if (errorreturned) { // handle error. } else { nserror *jsonparsingerror = nil; nsarray *jsonarray = [nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutablecontainers|nsjsonreadingallowfragments error:&jsonparsingerror]; }
this returning me info as:
{ d = "[{\"training_code\":\"1234 \",\"training_duration\":\"2hrs \",\"training_startdate\":\"14/02/2013 15:00:00\",\"training_enddate\":\"14/02/2013 17:00:00\",\"trainer_id\":1,\"training_location\":\"b-wing training room-4\",\"comments\":\"c# training\",\"keyword\":\"c#1234\",\"numberofdays\":1},{\"training_code\":\"4321 \",\"training_duration\":\"16 \",\"training_startdate\":\"17/02/2013 10:30:00\",\"training_enddate\":\"17/02/2013 17:30:00\",\"trainer_id\":2,\"training_location\":\"a-wing training room-6\",\"comments\":\"objective-c\",\"keyword\":\"obj-c4321\",\"numberofdays\":2}]"; }
this not in right format. want create this:
[{"training_code":"1234 ","training_duration":"2hrs ","training_startdate":"14/02/2013 15:00:00","training_enddate":"14/02/2013 17:00:00","trainer_id":1,"training_location":"b-wing training room-4","comments":"c# training","keyword":"c#1234","numberofdays":1},{"training_code":"4321 ","training_duration":"16 ","training_startdate":"17/02/2013 10:30:00","training_enddate":"17/02/2013 17:30:00","trainer_id":2,"training_location":"a-wing training room-6","comments":"objective-c","keyword":"obj-c4321","numberofdays":2}
note: web service returning proper json format.
what additional things need accomplish this.please suggest.
you web service returning proper json, but it's not. should talk web services developer , find out why they're returning malformed json. put, getting web service cannot parsed json--unless string extraction (ugh).
consider response data, improve formatted:
{ d = " [ { \"training_code\":\"1234 \", \"training_duration\":\"2hrs \", \"training_startdate\":\"14/02/2013 15:00:00\", \"training_enddate\":\"14/02/2013 17:00:00\", \"trainer_id\":1, \"training_location\":\"b-wing training room-4\", \"comments\":\"c# training\", \"keyword\":\"c#1234\", \"numberofdays\":1 }, { \"training_code\":\"4321 \", \"training_duration\":\"16 \", \"training_startdate\":\"17/02/2013 10:30:00\", \"training_enddate\":\"17/02/2013 17:30:00\", \"trainer_id\":2, \"training_location\":\"a-wing training room-6\", \"comments\":\"objective-c\", \"keyword\":\"obj-c4321\", \"numberofdays\":2 } ] "; }
first, convert info response string:
nsstring *json = [[nsstring alloc] initwithdata:data encoding:nsutf8stringencoding];
then need strip off top:
{ d = "
and bottom
"; }
then can replace of escaped quotes using code:
json = [json stringbyreplacingoccurrencesofstring:@"\\\"" withstring:@"\""];
at point, string should parsable json this:
[ { "training_code":"1234 ", "training_duration":"2hrs ", "training_startdate":"14/02/2013 15:00:00", "training_enddate":"14/02/2013 17:00:00", "trainer_id":1, "training_location":"b-wing training room-4", "comments":"c# training", "keyword":"c#1234", "numberofdays":1 }, { "training_code":"4321 ", "training_duration":"16 ", "training_startdate":"17/02/2013 10:30:00", "training_enddate":"17/02/2013 17:30:00", "trainer_id":2, "training_location":"a-wing training room-6", "comments":"objective-c", "keyword":"obj-c4321", "numberofdays":2 } ]
so can this:
id object = [nsjsonserialization jsonobjectwithdata:[json datausingencoding:nsutf8stringencoding] options:0 error:&error];
now object
should hold array containing 2 records dictionaries.
if me, though, go web services developer , demand prepare response they're sending you. shouldn't have deal of string extraction nonsense.
iphone ios objective-c json nsjsonserialization
No comments:
Post a Comment