ios - Performing GET with JSON data using NSDictionary -
i trying data using nsdictionary. data looks this:
{ "id": 1, "items": [ { "id": 1, "correctanswer": "sample string 3", "difficulty": 4, "categoryid": 5, "categoryname": "sample string 6", "accountid": 7 }, { "id": 1, "picurl": "sample string 2", "correctanswer": "sample string 3", "difficulty": 4, "categoryid": 5, "categoryname": "sample string 6", "accountid": 7 } "starttime": "2013-10-07t00:24:22.0048045+00:00" }
this tried:
- (ibaction)play:(id)sender { [self performseguewithidentifier:@"playsegue" sender:self]; nsstring *gameid; nsnumber *id; nsurl *pictureurl; nsstring *ans1; nsstring *ans2; nsstring *ans3; nsstring *ans4; nsarray *answers = [[nsarray alloc]initwithobjects:ans1,ans2,ans3,ans4, nil]; nsstring *correctanswer; nsstring *difficulty; nsstring *categoryid; nsstring *categoryname; nsnumber *accountid; nsarray *items = [[nsarray alloc]initwithobjects:id,pictureurl,answers,correctanswer,categoryid,categoryname,accountid, nil]; nsdictionary *result = [self requestwithpath:[nsstring stringwithformat:@"someurlhere"] method:@"get" data:[nsdictionary dictionarywithobjectsandkeys: gameid,@"gameid", items, @"items", nil] parse:yes]; nslog(@"the result %@",result); } -(nsdictionary *)requestwithpath:(nsstring *)path method:(nsstring *)requestmethod data:(nsdictionary *)requestdata parse:(bool)parse { if(path) nslog(@"%@", path); if(requestdata) nslog(@"%@", requestdata); nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:[nsurl urlwithstring:[[nsstring stringwithformat:@"%@", path] stringbyaddingpercentescapesusingencoding:nsutf8stringencoding]]]; [request sethttpmethod:requestmethod.uppercasestring]; [request setvalue:@"application/json" forhttpheaderfield:@"accept"]; if(requestdata && [nsjsonserialization isvalidjsonobject:requestdata]) { nserror *error = nil; nsdata *data = [nsjsonserialization datawithjsonobject:requestdata options:0 error:&error]; [request sethttpbody:data]; [request setvalue:@"application/json" forhttpheaderfield:@"content-type"]; [request setvalue:[nsstring stringwithformat:@"%d", [data length]] forhttpheaderfield:@"content-length"]; } nsurlresponse *response = nil; nsdata *responsedata = [nsurlconnection sendsynchronousrequest:request returningresponse:&response error:null]; if(responsedata) nslog(@"%@", [[nsstring alloc] initwithdata:responsedata encoding:nsutf8stringencoding]); if(parse && responsedata) { nserror *serializationerror = nil; return [nsjsonserialization jsonobjectwithdata:responsedata options:nsjsonreadingmutableleaves error:&serializationerror]; } else if (responsedata) return @{@"string": [[nsstring alloc] initwithdata:responsedata encoding:nsutf8stringencoding]}; return nil; }
console result
2013-10-06 19:01:16.477 flickrest[41610:a0b] someurlhere 2013-10-06 19:01:16.478 flickrest[41610:a0b] { } 2013-10-06 19:01:17.186 flickrest[41610:a0b] result (null)
my question
aside not getting result want, believe not doing in right way. point out mistake or suggest way how can convert json data properly? thank you.
just try read this , try use debug.
ps. code incorrect lot.
Comments
Post a Comment