Posts
417
Comments
233
Trackbacks
42
Using JSON.NET To Load Twitter Search Results In WP7

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"];

this.Items.Clear();

foreach (var tweet in results)
{
this.Items.Add(new TweetViewModel(tweet.SelectToken("profile_image_url").Value<string>(),tweet.SelectToken("text").Value<string>()));
}

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.

posted on Wednesday, December 14, 2011 7:27 AM Print
Comments
Gravatar
# re: Using JSON.NET To Load Twitter Search Results In WP7
Bil Simser
12/15/2011 8:36 AM
Nice article. You might find the online C# generator useful. Just plug in a JSON result or a JSON url and it'll provide C# classes for you. This gives you the advantage over a JObject so you'll have a nice rich object to interact with. http://json2csharp.com/
Gravatar
# re: Using JSON.NET To Load Twitter Search Results In WP7
Tim
12/18/2011 10:53 PM
Bil,

Thanks for the suggestion.

Post Comment

Title *
Name *
Email
Comment *  
Verification

Tim Murphy

Tim is a Solutions Architect for PSC Group, LLC. He has been an IT consultant since 1999 specializing in Microsoft technologies. Along with running the Chicago Information Technology Architects Group and speaking on Microsoft and architecture topics he was also contributing author on "The Definitive Guide to the Microsoft Enterprise Library".



I review for the O'Reilly Blogger Review Program



Technorati Profile

www.flickr.com
Tag Cloud