objective c - how to Parse complicated JSON data in iOS -
i using php json data , here response.
{"array":[{"category":{"charity_id":"2","charity_name":"givedirectly","charity_amt":"0.20"}}]}
and here objective-c code
nserror *err; nsurl *url=[nsurl urlwithstring:@"url"]; nsurlrequest *req=[nsurlrequest requestwithurl:url]; nsdata *data = [nsurlconnection sendsynchronousrequest:req returningresponse:nil error:&err]; nsdictionary *json = [nsjsonserialization jsonobjectwithdata:data options:0 error:&err]; if ([json iskindofclass:[nsdictionary class]]){ nsarray *yourstaffdictionaryarray = json[@"array"]; if ([yourstaffdictionaryarray iskindofclass:[nsarray class]]){ (nsdictionary *dictionary in yourstaffdictionaryarray) { for(nsdictionary *dict in dictionary[@"category"]){ nslog(@"%@",[[((nsstring*)dict) componentsseparatedbystring:@"="] objectatindex:0] ); } } } }
but returns name's not value. have searched of question on site nothing helped be. please me new ios.
thanks
dictionary output is
{ "charity_amt" = "0.20"; "charity_id" = 2; "charity_name" = givedirectly; }
you don't need componentseparatedbystring. once nsdictionary @"category", can value using keys.
something
if ([json iskindofclass:[nsdictionary class]]){ nsarray *yourstaffdictionaryarray = json[@"array"]; if ([yourstaffdictionaryarray iskindofclass:[nsarray class]]){ (nsdictionary *dictionary in yourstaffdictionaryarray) { nsdictionary *dict = dictionary[@"category"]; nslog(@"%@",dict[@"charity_id"]); } } }
Comments
Post a Comment