Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Using JSON.NET To Load Twitter Search Results In WP7

DZone's Guide to

Using JSON.NET To Load Twitter Search Results In WP7

· Mobile Zone
Free Resource

Discover how to focus on operators for Reactive Programming and how they are essential to react to data in your application.  Brought to you in partnership with Wakanda

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.


Source:  http://geekswithblogs.net/tmurphy/archive/2011/12/14/using-json.net-to-load-twitter-search-results-in-wp7.aspx

Learn how divergent branches can appear in your repository and how to better understand why they are called “branches".  Brought to you in partnership with Wakanda

Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}