Kash Farooq's software development blog

.NET Developer

Creating dynamic objects from JSON in .NET 4

Posted by Kash Farooq on July 23, 2012

I’m seeing quite a lot of people arriving at this blog with searches such as “datacontractjsonserializer .net 4”.

They end up at one of my old posts: Creating .NET objects from JSON using DataContractJsonSerializer. Implementing this will work in .NET 4, but why would you want to use DataContractJsonSerializer when you can use .NET 4 dynamic objects instead?

So, here is a very short blog post describing how to do just that.

I’m using some JSON I got from the Rotten Tomatoes API (which is excellent by the way). The JSON is all in one line so click hover over it and click the View Source icon to see it in a scrollable pop-up window.

{"id":11292,"title":"Star Wars: Episode IV - A New Hope","year":1977,"genres":["Action & Adventure","Mystery & Suspense","Science Fiction & Fantasy"],"mpaa_rating":"PG","runtime":105,"critics_consensus":"A legendarily expansive and ambitious start to the sci-fi saga, George Lucas opened our eyes to the possiblites of blockbuster filmmaking and things have never been the same.","release_dates":{"theater":"1977-05-25","dvd":"2004-09-21"},"ratings":{"critics_rating":"Certified Fresh","critics_score":94,"audience_rating":"Upright","audience_score":93},"synopsis":"","posters":{"thumbnail":"http://content9.flixster.com/movie/10/94/47/10944715_mob.jpg","profile":"http://content9.flixster.com/movie/10/94/47/10944715_pro.jpg","detailed":"http://content9.flixster.com/movie/10/94/47/10944715_det.jpg","original":"http://content9.flixster.com/movie/10/94/47/10944715_ori.jpg"},"abridged_cast":[{"name":"Mark Hamill","id":"162665747","characters":["Luke Skywalker"]},{"name":"Harrison Ford","id":"162661579","characters":["Han Solo"]},{"name":"Carrie Fisher","id":"162663355","characters":["Princess Leia Organa"]},{"name":"Peter Cushing","id":"162658723","characters":["Grand Moff Tarkin"]},{"name":"Alec Guinness","id":"162655900","characters":["Ben Obi-Wan Kenobi"]}],"abridged_directors":[{"name":"George Lucas"}],"studio":"20th Century Fox","alternate_ids":{"imdb":"0076759"},"links":{"self":"http://api.rottentomatoes.com/api/public/v1.0/movies/11292.json","alternate":"http://www.rottentomatoes.com/m/star_wars/","cast":"http://api.rottentomatoes.com/api/public/v1.0/movies/11292/cast.json","clips":"http://api.rottentomatoes.com/api/public/v1.0/movies/11292/clips.json","reviews":"http://api.rottentomatoes.com/api/public/v1.0/movies/11292/reviews.json","similar":"http://api.rottentomatoes.com/api/public/v1.0/movies/11292/similar.json"}}

I have put the above JSON into a text file which I’ll access via a test:

public void DeserializeJsonWithDynamic()
  string starWarsAsJson = File.ReadAllText(@"DataGateways\RottenTomatoes\StarWars.json");
  dynamic starWarsAsDynamic = Json.Decode(starWarsAsJson);
  Console.WriteLine("Title: "+starWarsAsDynamic.Title);
  Console.WriteLine("Year: " + starWarsAsDynamic.Year);
  Console.WriteLine("Original Poster: " + starWarsAsDynamic.Posters.Original);

In the above code I’m using Json.Decode, which is in System.Web.Helpers, to create a dynamic object out of the JSON text.

Running the test results in the following output:

Title: Star Wars: Episode IV - A New Hope
Year: 1977
Original Poster: http://content9.flixster.com/movie/10/94/47/10944715_ori.jpg

Very cool. Notice I went down the JSON object hierarchy to get Posters.Original.


2 Responses to “Creating dynamic objects from JSON in .NET 4”

  1. You told that without JSON this is possible but still you have make use of some of the portion of JSON. Is this the only way to make use of .NET 4 dynamic objects?

    • Not sure what you mean…
      I said you could decode JSON in .NET 4 without using the DataContractJsonSerializer. I never said I was going to show how to decode JSON without using the JSON library…

Sorry, the comment form is closed at this time.

%d bloggers like this: