Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 16th, 2012  |  syntax: None  |  size: 1.72 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. issue in parsing json and displaying data
  2. NSDictionary* profile = [profileinfo objectAtIndex:0]; //throws exception
  3.        
  4. //Json Method
  5.  
  6. {
  7.  
  8. "createdBy":"superadmin",
  9. "createdOn":"2011-11-15T00:49:06+05:30",
  10. "updatedBy":"superadmin",
  11. "updatedOn":"2011-11-15T00:49:06+05:30",
  12. "contactNumber":"9945614074",
  13. "emailNotification":"true",
  14. "firstName":"resident2",
  15. "lastName":"user5",
  16. "loginId":"jin",
  17. "married":"false",
  18. "message":"",
  19. "preferredLanguage":"ko_KR",
  20. "sex":"0",
  21. "smsNotification":"false",
  22. "status":"ACTIVE",
  23. "subscribedPlans":"Intelligent Concierge",
  24. "userName":"Cisco"
  25.  
  26. }
  27.  
  28. //code
  29.  
  30. - (void)loadData
  31.  
  32. {
  33.  
  34.  dataWebService = [[NSMutableData data] retain];
  35.  
  36.  
  37. NSURLRequest *request = [[NSURLRequest requestWithURL:[NSURL URLWithString:@"URL LINK"]]retain];  
  38.  
  39. [[NSURLConnection alloc]initWithRequest:request delegate:self];
  40.  
  41. }
  42.  
  43.  
  44. -(void)connectionDidFinishLoading:(NSURLConnection *)connection
  45. {
  46.  
  47.  
  48. [connection release];    
  49. NSString *responseString = [[NSString alloc] initWithData:dataWebService encoding:NSUTF8StringEncoding];
  50.  
  51. self.dataWebService = nil;
  52.  
  53. NSArray* profileinfo = [(NSDictionary*) [responseString JSONValue] objectForKey:@"createdBy"];
  54. [responseString release];    
  55.  
  56. NSDictionary* profile = [profileinfo objectAtIndex:0];
  57.  
  58. //fetch the data
  59. NSNumber* numb = [profile objectForKey:@"contactNumber"];
  60.  
  61. NSString* name = [profile objectForKey:@"firstName"];
  62.  
  63. //set the text to the label
  64. label.numberOfLines = 0;
  65. label.text = [NSString stringWithFormat:@"contactNumber: %@ n n Name:  %@ n n",
  66.              numb,name];
  67.  
  68. }
  69.        
  70. NSArray* profileinfo = [(NSDictionary*) [responseString JSONValue] objectForKey:@"createdBy"];
  71.        
  72. NSDictionary* profile = [profileinfo objectAtIndex:0];
  73.        
  74. NSDictionary * profile =  (NSDictionary*)[responseString JSONValue];