Json.Net Parsing Example

I have been looking at how to easily consume json from C#.
Given the source of the data I don’t want to create specific types (I’ll be creating a dynamic wpf control to allow editing of json read from a configuration database).
This requires newtonsoft.json to be pulled from nuget and a reference made to newtonsoft.json.


using System;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace SimpleJsonTest
{
class Program
{
static void Main(string[] args)
{

string json = @"[{""First"":""Chris"",""LastName"":""Pietschmann""}, {""First"":""Chris"",""LastName"":""Eyre""}]";
List<Dictionary> values = JsonConvert.DeserializeObject<List<Dictionary>>(json);

Console.WriteLine( string.Join(",", values[0].Keys));
Console.WriteLine(string.Join(",", values[0].Values));
Console.WriteLine(string.Join(",", values[1].Values));

Console.ReadLine();
}
}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s