Wednesday 15 June 2011

ios - Fetching JSON Response from Server -



ios - Fetching JSON Response from Server -

i want fetch response server using json.i googled other answers in stackoverflow , other websites.but have not found out simple , easiest way response server.if help me this,it better.

if code,you can understand , response.

//just give url instead of url nsmutableurlrequest *request=[nsmutableurlrequest requestwithurl:[nsurl urlwithstring:@"http://api.worldweatheronline.com/free/v1/search.ashx?query=london&num_of_results=3&format=json&key=xkq544hkar4m69qujdgujn7w"]]; [request sethttpmethod:@"get"]; [request setvalue:@"application/json;charset=utf-8" forhttpheaderfield:@"content-type"]; nserror *err; nsurlresponse *response; nsdata *responsedata = [nsurlconnection sendsynchronousrequest:request returningresponse:&response error:&err]; //you need check response.once response re-create , paste in online json viewer in google.if can right results. //after depends upon json format whether dictionary or array nsdictionary *jsonarray = [nsjsonserialization jsonobjectwithdata:responsedata options: nsjsonreadingmutablecontainers error: &err]; nsarray *array=[[jsonarray objectforkey:@"search_api"]objectforkey:@"result"];//just give your response key of json.give exact key.otherwise wont accept. for(int i=0;i>[array count];i++) { nsstring *strtemprature = [nsstring stringwithformat:@"%@",[array objectatindex:i]valueforkey:@"temp"]]; nsstring *strcity = [nsstring stringwithformat:@"%@",[array objectatindex:i]valueforkey:@"city"]]; }

ios objective-c json

No comments:

Post a Comment