The main way that Twitter provides feeds is through JSON. Unfortunately the version of the .NET Framework that is on the phone does not include the System.Runtime.Serialization.Json namespace. Rather than trying to brute force the JSON feed I have been looking into using the Newtonsoft JSON.NET library.
The first thing that I ran into with JSON.NET is that the documentation is severely tilted to the serialization side with almost no mention of deserialization. This would have been my preferred approach considering work I have done with WCF. In a future posts I intend to explore deserialization in JSON.NET. Given my time constraints though I ended up using the JSON to LINQ feature of JSON.NET.
Once I headed down this new road I found that even the LINQ features did not work the way I would have expected them to. I would have thought that when the library parsed the JSON feed it would be able to drill down like a normal object graph. In the end I used the base JSON to LINQ ability to get a named set and then loop through it using the SelectToken method to pull individual values.
Let’s take a look at an example. The first thing you need to do is open a WebClient object and assign a method to handle the DownloadStringAsync event and call the DownloadStringAsync method.
WebClient client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
client.DownloadStringAsync(new Uri("http://search.twitter.com/search.json?q=" + searchString));
Now comes the real work. You will see that a JObject is used to parse the feed returned from Twitter. After that you are able to get the “results” array by name. I was concerned with getting the text and user image so while looping through the results I access the profile_image_url and text fields of each and record the values.
JObject searchResult = JObject.Parse(e.Result);
var results = searchResult["results"];
foreach (var tweet in results)
Once I have this List of TweetViewModel items I can bind it to a templated ListBox to render it to the user. In all this was a very simple piece of code that took a lot of trail and error because of poor documentation. As I dig deeper into this library I will do my best to augment the official documentation.