JSON Array to C# Using Json.Net

by Nate on November 20, 2008

Json.Net is a great little library that “makes working with JavaScript and JSON formatted data in .Net simple”. I’ve been using Json.Net for quite some time now, but have restricted my use mostly to building JSON strings in C# and pushing them down to the client. It works well for this, although I’ll admit that I don’t think that manually building JSON strings in C# will ever be anything other than tedious.

Well, today I starting working on a query framework for NPMap, and decided that JSON was the easiest (and most efficient) way to pass parameters for the queries up to the server. You see, the user interface allows the user to query one or more database entities and choose one or more (or all) fields from each entity to include in the query. So I decided to send up an array of JavaScript objects that looked a little something like this:

[
  {
    "entity": "Battlefields",
    "fields": "BattlefieldCode, BattlefieldName, StartDate, EndDate"
  },
  {
    "entity": "CivilRightsSites",
    "fields": "SiteId, StartDate, EndDate"
  }
]

But how to deserialize that JSON string into something I can use on the server? Enter Json.Net.

First I created a class to hold each of the JavaScript objects:

public class QueryEntity
{
     public string entity {get; set; }
     public string fields {get; set; }
}

Next I deserialized the JSON string into a list of QueryEntity objects:

List<QueryEntity> entities = (List<QueryEntity>) JavaScriptConvert.DeserializeObject(json, typeof(List<QueryEntity>));

And finally I iterated through the objects:

foreach (QueryEntity obj in entities)
{
     string entity = obj.entity;
     string fields = obj.fields;
}

And that’s it. Json.Net saves the day again!

{ 9 comments… read them below or add one }

scott November 21, 2008 at 9:39 am

this NPMap for the National Park Service sounds interesting. Care to elaborate on it and the technology you are using?

Reply

Nate November 21, 2008 at 10:18 am

Hi Scott, NPMap is a project that I’ve been working on for the last six months or so. It is basically a web mapping platform that enables all NPS web map applications to share the same set of resources, including data services and code.

I’ve been meaning to write about it for some time now, so I’ll try to get to it in my next blog post. But, in short, NPMap is really just a bunch of JavaScript that is generated on the server (ASP.Net, C#) and then pushed down to the client. All “applications” within NPMap are driven from configs stored in a database, so getting an “application” up and running is a matter of going into the database and setting up the features. It takes about five minutes to get a new application up with tools, services, etc.

We’re using a bunch of different technologies to drive the system, including SQL Server 2008 spatial, ArcGIS Server, the ArcDeveloper tile server, a bunch of REST APIs (including some of the web services offered by GeoNames), Microsoft Virtual Earth, Ext JS, jQuery, and a number of internal NPS databases.

That’s the short description. It is a very interesting project, and I should be working on it for sometime to come. The best part about it is its modular nature; I can add new technologies into the mix without breaking anything.

Reply

scott November 21, 2008 at 2:05 pm

sounds cool Nate!

Reply

charleen October 7, 2009 at 3:05 am

Great post!
Thank You!!

Reply

simi December 30, 2009 at 12:12 am

hi,

First of all. Thanks very much for your useful post.

I just came across your blog and wanted to drop you a note telling you how impressed I was with the information you have posted here.

Please let me introduce you some info related to this post and I hope that it is useful for .Net community.

There is a good C# resource site, Have alook

http://www.csharptalk.com/2009/09/c-array.html
http://www.csharptalk.com/2009/10/creating-arrays.html

simi

Reply

abhi May 19, 2010 at 6:23 am

var items[];
for(i=0 to 3)
{
var item={
id : i,
name: name+i,
};
}
$.post(‘updatePanels.aspx’, ‘data=’+$.toJSON(sortorder), function(response){ });

i wnt to convert ths in c#..
plz tell me which json file to use in c# to extract the abv items[]

ref:

Reply

abhi May 19, 2010 at 6:26 am

plz forgive minor chng in for loop
for()
{
items.push(item);
}
http://webdeveloperplus.com/jquery/saving-state-for-collapsible-drag-drop-panels/

Reply

teebar October 18, 2010 at 6:38 pm

In case anyone comes across this post and tries to use the code:

JavaScriptConvert has been renamed to JsonConvert in json.Net

Reply

Daniel September 15, 2011 at 11:57 am

@teebar:

Thanks for pointing this out. Have been searching upside down for any decent examples of Json.net usage, as the documentation is still _seriously_ lacking.

Reply

Leave a Comment

Previous post:

Next post: