Geeks With Blogs

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

Tim Murphy's .NET Software Architecture Blog Adventures in Architecting and Developing .NET

In a previous post I talked about using the JSON to LINQ features of JSON.NET.  Thanks to Bil Simser I was able to do some testing of the deserialization capabilities of this library.  The key was his hint to leverage the json2csharp web site.  Below is the C# representation of the search results for a Twitter query created by putting an example of the search result into this site. 

public class Url
public string url { get; set; }
public string expanded_url { get; set; }
public string display_url { get; set; }
public int[] indices { get; set; }

public class Entities
public Url[] urls { get; set; }

public class Metadata
public int recent_retweets { get; set; }
public string result_type { get; set; }

public class Result
public string created_at { get; set; }
public Entities entities { get; set; }
public string from_user { get; set; }
public int from_user_id { get; set; }
public string from_user_id_str { get; set; }
public object geo { get; set; }
public object id { get; set; }
public string id_str { get; set; }
public string iso_language_code { get; set; }
public Metadata metadata { get; set; }
public string profile_image_url { get; set; }
public string source { get; set; }
public string text { get; set; }
public object to_user_id { get; set; }
public object to_user_id_str { get; set; }

public class RootObject
public double completed_in { get; set; }
public long max_id { get; set; }
public string max_id_str { get; set; }
public string next_page { get; set; }
public int page { get; set; }
public string query { get; set; }
public string refresh_url { get; set; }
public Result[] results { get; set; }
public int results_per_page { get; set; }
public int since_id { get; set; }
public string since_id_str { get; set; }

So how do we use this class once it has been created?  First we need to make sure that we are referencing the Newtonsoft.Json assembly.  As with the previous example we will use the WebClient DownloadStringAsync method to call Twitter.  The main difference is the small bit of call that is in the callback function. 

In the example below I have created a JsonSerializer and then feed the Deserialize method with a new JsonTextReader and the type of the JSON message class.  Once we get to that point it is a simple act of iterating through the object graph to find the members you are interested in.

JsonSerializer serializer = new JsonSerializer();
RootObject rootObject = (RootObject)serializer.Deserialize(new JsonTextReader(new StringReader(e.Result)), typeof(RootObject));

foreach (var tweet in rootObject.results)
this.Items.Add(new TweetViewModel(tweet.profile_image_url, tweet.text));

Between the JSON.NET library and the json2csharp web site the process of adding JSON deserialization to your Windows Phone app is much simpler.  Now go and put it to good use.

Posted on Friday, January 13, 2012 12:28 PM Windows Phone 7 , JSON | Back to top

Comments on this post: Twitter Search JSON Deserialization

# re: Twitter Search JSON Deserialization
Requesting Gravatar...
I tried to use this series of code to create a password generator but I couldn't. I know that the code usually used for password generation doesn't look like this but I wanted to make a new hybrid code. If you have some indications on this matter I would love to hear them.
Left by Neil on May 13, 2012 4:48 AM

# re: Twitter Search JSON Deserialization
Requesting Gravatar...
Fantastic! Saved me a lot of time, thanks!
Left by Paul on Jul 19, 2012 5:42 AM

Your comment:
 (will show your gravatar)

Copyright © Tim Murphy | Powered by: | Join free