LINQ to JASON

public class Shortie
{
  public string Original { get; set; }
  public string Shortened { get; set; }
  public string Short { get; set; }
  public ShortieException Error { get; set; }
}
 
public class ShortieException
{
  public int Code { get; set; }
  public string ErrorMessage { get; set; }
}

Manually serializing and deserializing between .NET objects is most useful when working with JSON that doesn't closely match your .NET objects.
string jsonText = @"{
  ""short"":{
    ""original"":""http://www.foo.com/"",
    ""short"":""krehqk"",
    ""error"":{
      ""code"":0,
      ""msg"":""No action taken""}
}";
 
JObject json = JObject.Parse(jsonText);
 
Shortie shortie = new Shortie
                  {
                    Original = (string)json["short"]["original"],
                    Short = (string)json["short"]["short"],
                    Error = new ShortieException
                            {
                              Code = (int)json["short"]["error"]["code"],
                              ErrorMessage = (string)json["short"]["error"]["msg"]
                            }
                  };
 
Console.WriteLine(shortie.Original);
// http://www.foo.com/
 
Console.WriteLine(shortie.Error.ErrorMessage);
Ebook Download
View all
Learn
View all