27

I receive the following Json through a web service:

  {
     report: {
      Id: "aaakkj98898983"
     }
  }

I want to get value of the Id. How to do this in C#? THANKS

Tim B James
  • 19,853
  • 4
  • 73
  • 99

1 Answers1

84

First, download Newtonsoft's Json Library, then parse the json using JObject. This allows you to access the properties within pretty easily, like so:

using System;
using Newtonsoft.Json.Linq;

namespace testClient
{
    class Program
    {
        static void Main()
        {
            var myJsonString = "{report: {Id: \"aaakkj98898983\"}}";
            var jo = JObject.Parse(myJsonString);
            var id = jo["report"]["Id"].ToString();
            Console.WriteLine(id);
            Console.Read();
        }
    }
}   
Maloric
  • 5,312
  • 2
  • 28
  • 46