coolerfarmer

Untitled

Oct 18th, 2014
5,764
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Net;
  5. using System.Web.Script.Serialization;
  6.  
  7. namespace JsonDeserialize
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. string webResponseString = DownloadSkinGuidFromElophant("55522751");
  14. Console.WriteLine(webResponseString);
  15. string json = ExtractGuid(webResponseString);
  16. Console.WriteLine("GUID: {0}", json);
  17. Console.ReadKey();
  18. }
  19.  
  20. private static string DownloadSkinGuidFromElophant(string id)
  21. {
  22. string requestString = String.Format("http://internal.elophant.com/v1/getskinkey/na/{0}", id);
  23. try
  24. {
  25. return new WebClient().DownloadString(requestString);
  26. }
  27. catch (Exception ex)
  28. {
  29. #if DEBUG
  30. Debug.WriteLine(ex);
  31. #endif
  32. return "";
  33. }
  34. }
  35.  
  36. private static string ExtractGuid(string rawJson)
  37. {
  38. //Add a reference to System.Web.Extensions and import the namespace System.Net and System.Web.Script.Serialization
  39. var serializer = new JavaScriptSerializer();
  40. var deserializeJson = serializer.Deserialize<Dictionary<string, string>>(rawJson);
  41. if(!deserializeJson.ContainsKey("guid")) throw new Exception("Raw Json has the wrong format");
  42. return deserializeJson["guid"];
  43. }
  44. }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment