Advertisement
lseidman

Deserializing with WP7 C#

Dec 29th, 2011
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. #########################################################################################################
  2. *** DESIGNED/WRITTEN REGARDING FOURSQUARE API AS AN EXAMPLE BY LANCE SEIDMAN ***
  3.  
  4. The API will NOT work without signing up as a Dev and will NOT work without Authorizing via OAuth. No
  5. current work-around as far as OAuth as it authorizes access to the API for your App.
  6.  
  7. Using the coding structure provided assumes you've already forced Auth and can now perform API Tasks.
  8. #########################################################################################################
  9.  
  10. 1). Choose the URL you're trying to use for your specific section of your App, in this case I have chosen to
  11. get/use a Users Profile (accessing the defined ID for details about the account).
  12.  
  13. + URL: https://api.foursquare.com/v2/users/<ID_NUMBER>?oauth_token=<Auth_Code_Here>
  14.  
  15. 2). In this segment, I want to get the User First/Last Name and Gender. Below will show you what must be done.
  16.  
  17. We need to setup classes and I assume you already know how to do the basics and grab the contents using WebClient of the URL. With that being said, your classes should look similar to this:
  18.  
  19. public class UserResults
  20. {
  21. public response[] response { get; set; } // Looking for response and it's content.
  22. }
  23.  
  24. public class response
  25. {
  26. user[] user { get; set; } // This says we are looking for "user" content/section.
  27. }
  28.  
  29. public class user
  30. {
  31. int id { get; set; } // This is the "ID" of the profile/user.
  32. string firstName { get; set; } // This is the First Name Field.
  33. string lastName { get; set; } // This is the Last Name Field.
  34. string gender { get; set; } // This is the Gender Field (Result: male/female)
  35. }
  36.  
  37. 3). Remember to use WebClient, it is as easy as this:
  38.  
  39. WebClient client = new WebClient(); // Creates a new instance and calls it "client" for accessing.
  40. client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted); /* Send data
  41. to a specific function once the event has finished (completed). */
  42. client.OpenReadAsync(new Uri("https://api.foursquare.com/v2/users/<ID_NUMBER>?oauth_token=<Auth_Code_Here>")); // This inserts the URI for it to grab the Data from.
  43.  
  44. 4). That is it... Now, if you're not sure how to really deserialize easily, I have put one of the better ways below NOT USING JSON.NET.
  45.  
  46. var serializer = new DataContractJsonSerializer( typeof(UserResults) );
  47. var infoGrab = (UserResults)serializer.ReadObject( eargs.Result ); // Some cases it could be e.Result.
  48.  
  49. Tip: Why have I used the eargs opposed to e.Result? I normally do a lambda statement where I have a callback function that looks like:
  50.  
  51. client.OpenReadCompleted += (s, eargs ) =>
  52. {
  53. // DeSerializing Code Here
  54. };
  55.  
  56. var uri = new Uri("<Address to API>");
  57. client.OpenReadAsync( uri );
  58. }
  59.  
  60. 5). You're done! Good luck and enjoy your future programming with C# and WP7 but most of all, thanks for being apart of the UserCommunity with programmers like you, myself and others alike! Remember to thank L.K. as she was the one who asked :).
  61.  
  62. Sincerely,
  63. Lance Seidman
  64. Blog: http://lance.compulsivetech.biz
  65. Twitter: @LanceSeidman
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement