Using JSON (with asp.net) is like wiping your ass with silk

icon

I love mashups. Actually, I love everything about them, I love using them, I love making them, I love those who do everything they can to empower them. In my opinion, mashups are one of the most significant concepts the Web has invented, since they represent unlimited possibilities of integrating and reshaping things that are already done. The platforms out there are stable, so it's the creativity that sets the limits. These days, you can easily take data from anyone and do something else with it. Just don't forget to use JSON.

I'm an old school guy who has been working mostly in enterprise software environments, which means using XML for data integrations, besides, RSS used to be the man. The first generation of Twenity (Twitfluence) was using XML feeds, and this blog is using mostly RSS for fetching entries from other sources. XML was the standard we all spoke, very cute and readable, but on the other hand, not so easy to parse. At that time, this fact didn't represent such a major problem, since a few more lines of code took care of everything. But today is a different situation.

It was Twitter who first started dropping support for XML, which annoyed the hell out of me. I had to start rewriting things for JSON. This turned out to be one of the best things that ever happened, since JSON seems to be loved by everyone. It's super easy to use with jQuery, but since I'm a server-side type of a guy, it's even more important that it's cleverly integrated into asp.net. Fetch the data, store it, do crazy shit with it.

JSON is serializable into a .net object with a single line of code. Create the class with parameters compliant with the the specific JSON structure, serialize the response string into that class, and everything automagically works. Piece of cake, unlimited opportunities. Take Twitter for example:

Make the basic Tweet class:

public class Tweet
{
 public string id_str;
 public string text;
}

Create the request to access a tweet (funny, the hardest thing to do):

string url = "http://api.twitter.com/1/statuses/show/274508827146215424.json";
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = "GET";
WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string jsonResponse = reader.ReadToEnd();
reader.Close();

Which will return something like this (play here):

{
 "created_at": "Fri Nov 30 13:42:59 +0000 2012",
 "id_str": "274508827146215424",
 "text": ""Facebook knows what we say, Google knows what we think",
 "source": "web",
 ...
}

Serialize the response string into an object:

JavaScriptSerializer js = new JavaScriptSerializer();
Tweet tweet = new Tweet();
tweet = js.Deserialize<Tweet>(jsonResponse);
Response.Write(tweet.text);

Pretty neat. Since I've started using JSON, mashups have become easier than ever to make. With one of our latest projects, we've integrated our application with Twitter, Instagram, Foursquare and Flickr. For breakfast! XML may have its advantages, but for such things, JSON is simply the greatest. All hail the new lord!

Like what you read? Now tell the world!
Vote on Hacker News

A few more things you might find interesting:


Comment
written 5.1.2013 19:45 CET on chronolog
2491 views   •   Like   •   
date
date
date
date

Connect with :