The Simplest way to Parse JSON Data in C# using JSON.Net

SimpleJSONNetExample

using System;
using Newtonsoft.Json.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string json = "{\"employees\": [{ \"firstName\":\"John\" , \"lastName\":\"Doe\" },{ \"firstName\":\"Anna\" , \"lastName\":\"Smith\" }]}";

            JObject o = JObject.Parse(json);
            Console.WriteLine(o["employees"][0]);
            Console.WriteLine(o["employees"][1]["firstName"]);
            Console.WriteLine(o["employees"][1]["lastName"]);
            Console.ReadLine();
        }
    }
}

I was looking for a simple way to parse JSON data using C#, and was led to this example from MSDN: http://msdn.microsoft.com/en-us/library/bb412179(v=vs.110).aspx on how to Serialize and Deserialize JSON Data.  DataContracts, DataMembers, on and on, seemed a bit much for just trying to extract some data from a string.

JSON.net is a library from James Newton-King.  By far the easiest .NET JSON Parser available and according to James, it’s also the fastest.

Resources:
JSON.NET on CodePlex
http://json.codeplex.com/

An Introduction to JavaScript Object Notation (JSON) in JavaScript and .NET
http://msdn.microsoft.com/en-us/library/bb299886.aspx

 

 

Author: Travis Gneiting

Passionate Professional .NET Developer

2 thoughts on “The Simplest way to Parse JSON Data in C# using JSON.Net”

  1. Awesome!
    But how to serialize dataset or datatable to get a json string like your string value.

Comments are closed.