
Untitled
By: a guest on
May 16th, 2012 | syntax:
None | size: 1.72 KB | hits: 16 | expires: Never
issue in parsing json and displaying data
NSDictionary* profile = [profileinfo objectAtIndex:0]; //throws exception
//Json Method
{
"createdBy":"superadmin",
"createdOn":"2011-11-15T00:49:06+05:30",
"updatedBy":"superadmin",
"updatedOn":"2011-11-15T00:49:06+05:30",
"contactNumber":"9945614074",
"emailNotification":"true",
"firstName":"resident2",
"lastName":"user5",
"loginId":"jin",
"married":"false",
"message":"",
"preferredLanguage":"ko_KR",
"sex":"0",
"smsNotification":"false",
"status":"ACTIVE",
"subscribedPlans":"Intelligent Concierge",
"userName":"Cisco"
}
//code
- (void)loadData
{
dataWebService = [[NSMutableData data] retain];
NSURLRequest *request = [[NSURLRequest requestWithURL:[NSURL URLWithString:@"URL LINK"]]retain];
[[NSURLConnection alloc]initWithRequest:request delegate:self];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[connection release];
NSString *responseString = [[NSString alloc] initWithData:dataWebService encoding:NSUTF8StringEncoding];
self.dataWebService = nil;
NSArray* profileinfo = [(NSDictionary*) [responseString JSONValue] objectForKey:@"createdBy"];
[responseString release];
NSDictionary* profile = [profileinfo objectAtIndex:0];
//fetch the data
NSNumber* numb = [profile objectForKey:@"contactNumber"];
NSString* name = [profile objectForKey:@"firstName"];
//set the text to the label
label.numberOfLines = 0;
label.text = [NSString stringWithFormat:@"contactNumber: %@ n n Name: %@ n n",
numb,name];
}
NSArray* profileinfo = [(NSDictionary*) [responseString JSONValue] objectForKey:@"createdBy"];
NSDictionary* profile = [profileinfo objectAtIndex:0];
NSDictionary * profile = (NSDictionary*)[responseString JSONValue];