Parsing Twitter JSON data in C#
The Twitter API lets you return search results in either Atom or JSON. Atom is nice, because you can just throw it into an XMLDocument object and use XPath queries to get the results you’d like. But its obvious that the Tweets have been rather forcefully dumped into the various Atom fields and the JSON implementation is cleaner. But how do you parse that JSON in .Net?
There are loads of JSON serializers available for .Net, but very few parsers. It took me a while to find this one. It does the job well, but the JsonDecode() function has a rather unfriendly output which according to the comments can be:
/// An ArrayList, a Hashtable, a double, a string, null, true, or false
So which is it? Well the return type for a Twitter search query is a Hashtable:
HashTable jsonHash = (Hashtable)Procurios.Public.JSON.JsonDecode(jsonCode);
This contains a number of elements, including the search results themselves, which are in an element called [results]. The [results] element is actually an ArrayList:
ArrayList jsonResults = (ArrayList)jsonHash["results"];
foreach (object objResult in jsonResults) {
… and each ArrayList item is another Hashtable containing the details of each search result:
Hashtable jsonResult = (Hashtable)objResult;
System.Diagnostics.Debug.WriteLine("User ID: " + jsonResult["from_user_id"].ToString());
System.Diagnostics.Debug.WriteLine("Tweet text: " + jsonResult["text"].ToString());
System.Diagnostics.Debug.WriteLine("Tweet date: " + jsonResult["created_at"].ToString());
System.Diagnostics.Debug.WriteLine("User name: " + jsonResult["from_user"].ToString());
System.Diagnostics.Debug.WriteLine("Language: " + jsonResult["iso_language_code"].ToString())
The full code would be:
HashTable jsonHash = (Hashtable)Procurios.Public.JSON.JsonDecode(jsonCode);
ArrayList jsonResults = (ArrayList)jsonHash["results"];
foreach (object objResult in jsonResults)
{
Hashtable jsonResult = (Hashtable)objResult;
System.Diagnostics.Debug.WriteLine("User ID: "
+ jsonResult["from_user_id"].ToString());
System.Diagnostics.Debug.WriteLine("Tweet text: "
+ jsonResult["text"].ToString());
System.Diagnostics.Debug.WriteLine("Tweet date: "
+ jsonResult["created_at"].ToString());
System.Diagnostics.Debug.WriteLine("User name: "
+ jsonResult["from_user"].ToString());
System.Diagnostics.Debug.WriteLine("Language: "
+ jsonResult["iso_language_code"].ToString())
}
Obviously in production code you’d want to be a bit more careful than the test sample above (for example using GetType() to validate each step), but hopefully that gives you some insight into parsing Twitter’s JSON into C#. Once you’ve got your head around the Hashtable > ArrayList > Hashtable structure its actually very quick to use.


Awesome- saved me a lot of digging around and works a dream. Thanks for sharing.
Thank you for posting this. Just what I needed.
Awesome post… Ive found it after 5 hours of garbage searching. Thanx for the post!
I have another question.. can you help me with following Twitter accounts? I need urgent help ASAP.
Warm Regards,
Farrukh Javeid
There are loads of good resources here:
http://www.newwebplatform.com/tips-and-tutorials/Twitter
Make sure you use oAuth though, as standard HTTP authentication is about to expire.
Well that was really useful, thank you
[...] To implement twitter search in C# that uses oauth for authentication, first we need a C# oauth implementation, and a JSON parser for extracting results. For oauth implementation I’m using Twitter oAuth with .NET by Shannon whitley and for parsing the results JSON parser by Procurious and i have to say that this post is just a bit refined implementation of Parsing Twitter JSON data in C# by Jamie’s Digital Blog. [...]