php - UITextView prints JSON identifier instead of desired parsed data -
like title says, uitextview setup print data found within "username". instead of behaving appropriately, prints "username".
php code:
<?php header('content-type: application/json'); $arr = array ('username'=>'derekshull', 'userbio'=>'this derekshulls bio','usersubmitted'=>'15'); echo json_encode($arr); ?> objective c:
- (void)viewdidload { [super viewdidload]; nsurl *myurl = [[nsurl alloc]initwithstring:@"http://techinworship.com/json.php"]; nsdata *mydata = [[nsdata alloc]initwithcontentsofurl:myurl]; nserror *error; nsarray *jsonarray = [nsjsonserialization jsonobjectwithdata:mydata options:nsjsonreadingmutablecontainers error:&error]; if(!error) { (id element in jsonarray) { textview.text = [nsstring stringwithformat:@"%@", [element description]]; // text view contain last element loop } } else{ textview.text = [nsstring stringwithformat:@"error--%@",[error description]]; } } what missing here? also, when run, application not crash , give error. however, following documented in debug area.
2014-03-24 20:20:53.258 testtest[11434:60b] unknown class textview in interface builder file.
i don't have osx computer available can't evaluate code.
it seems array have in php code associative array , json similar. when parsing json string in obj-c code try assigning nsdictionary, way have access associative array.
nsdictionary *jsonarray = [nsjsonserialization jsonobjectwithdata:mydata options:nsjsonreadingmutablecontainers error:&error]; when using data in jsonarray don't iterate it, use nsdictionary method objectforkey get value want.
as example, value of username, can this:
[jsonarray objectforkey: @"username"] // jsonarray not array, dictionary and change text of textview, can following:
textview.text = [nsstring stringwithformat:@"%@", [jsonarray objectforkey: @"username"]]; ask if unclear!
cheers!
Comments
Post a Comment