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!
{ 5 comments… read them below or add one }
this NPMap for the National Park Service sounds interesting. Care to elaborate on it and the technology you are using?
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.
sounds cool Nate!
Great post!
Thank You!!
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