30

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
  • 20,084
  • 4
  • 73
  • 103

1 Answers1

91

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,525
  • 3
  • 31
  • 46