Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Net;
- using System.Web.Script.Serialization;
- namespace JsonDeserialize
- {
- class Program
- {
- static void Main(string[] args)
- {
- string webResponseString = DownloadSkinGuidFromElophant("55522751");
- Console.WriteLine(webResponseString);
- string json = ExtractGuid(webResponseString);
- Console.WriteLine("GUID: {0}", json);
- Console.ReadKey();
- }
- private static string DownloadSkinGuidFromElophant(string id)
- {
- string requestString = String.Format("http://internal.elophant.com/v1/getskinkey/na/{0}", id);
- try
- {
- return new WebClient().DownloadString(requestString);
- }
- catch (Exception ex)
- {
- #if DEBUG
- Debug.WriteLine(ex);
- #endif
- return "";
- }
- }
- private static string ExtractGuid(string rawJson)
- {
- //Add a reference to System.Web.Extensions and import the namespace System.Net and System.Web.Script.Serialization
- var serializer = new JavaScriptSerializer();
- var deserializeJson = serializer.Deserialize<Dictionary<string, string>>(rawJson);
- if(!deserializeJson.ContainsKey("guid")) throw new Exception("Raw Json has the wrong format");
- return deserializeJson["guid"];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment