<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Designer Silverlight &#187; New York Times API</title>
	<atom:link href="http://www.designersilverlight.com/category/new-york-times-api/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.designersilverlight.com</link>
	<description>Matthias Shapiro's Silverlight &#38; WPF Blog - Because Developers Get All The Good Blogs</description>
	<lastBuildDate>Thu, 26 Jan 2012 06:45:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Source Code For Presidential Candidate Tracker Visualization</title>
		<link>http://www.designersilverlight.com/2009/02/26/source-code-for-presidential-candidate-tracker-visualization/</link>
		<comments>http://www.designersilverlight.com/2009/02/26/source-code-for-presidential-candidate-tracker-visualization/#comments</comments>
		<pubDate>Fri, 27 Feb 2009 01:19:54 +0000</pubDate>
		<dc:creator>Matthias Shapiro</dc:creator>
				<category><![CDATA[c#]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[MIX09]]></category>
		<category><![CDATA[New York Times API]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://www.designersilverlight.com/?p=586</guid>
		<description><![CDATA[Due to repeated requests for the source code (and the fact that I apparently can&#8217;t brag about it on Silverlight.Net without a link to the source code), I&#8217;m putting it up for download. Presidential Candidate News Tracker Source Code JSON Data File With Candidate Data (Note: Apparently, WordPress likes to uncapitalize file names for me, [...]]]></description>
			<content:encoded><![CDATA[<p>Due to repeated requests for the source code (and the fact that I apparently can&#8217;t brag about it on Silverlight.Net without a link to the source code), I&#8217;m putting it up for download. </p>
<p><a href='http://www.designersilverlight.com/wp-content/uploads/2009/02/newyorktimessilverlightvisualization.zip'>Presidential Candidate News Tracker Source Code</a></p>
<p><a href='http://www.designersilverlight.com/wp-content/uploads/2009/02/canddata.json'>JSON Data File With Candidate Data</a> (Note: Apparently, WordPress likes to uncapitalize file names for me, you may have to re-name the file to &#8220;CandData.json&#8221; to get it to work with the app.)</p>
<p>Warning: This code is a disaster. I was having strange problems getting my custom controls to work and, after a couple hours fighting with it, I gave up and ended up writing the exact same layout and code for 14 separate candidates. Same problem with the &#8220;dates-of-note&#8221; along the timeline.</p>
<p>Not pretty, but it works. Have fun.</p>
<iframe src="http://www.facebook.com/plugins/like.php?href=http://www.designersilverlight.com/2009/02/26/source-code-for-presidential-candidate-tracker-visualization/&amp;layout=standard&amp;show_faces=1&amp;width=450&amp;action=like&amp;colorscheme=light&amp;font=" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:25px"></iframe><div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Source+Code+For+Presidential+Candidate+Tracker+Visualization+http%3A%2F%2Fdesignersilverlight.com%2F%3Fp%3D586" title="Post to Twitter"><img class="nothumb" src="http://www.designersilverlight.com/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter.png" alt="Post to Twitter" /></a> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Source+Code+For+Presidential+Candidate+Tracker+Visualization+http%3A%2F%2Fdesignersilverlight.com%2F%3Fp%3D586" title="Post to Twitter">Tweet This Post</a></p></div>]]></content:encoded>
			<wfw:commentRss>http://www.designersilverlight.com/2009/02/26/source-code-for-presidential-candidate-tracker-visualization/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Adventures with JSON and Silverlight (and the New York Times)</title>
		<link>http://www.designersilverlight.com/2009/02/25/adventures-with-json-and-silverlight-and-the-new-york-times/</link>
		<comments>http://www.designersilverlight.com/2009/02/25/adventures-with-json-and-silverlight-and-the-new-york-times/#comments</comments>
		<pubDate>Thu, 26 Feb 2009 00:17:56 +0000</pubDate>
		<dc:creator>Matthias Shapiro</dc:creator>
				<category><![CDATA[Advanced Tutorial]]></category>
		<category><![CDATA[Blend]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[How To...]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[New York Times API]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://www.designersilverlight.com/2009/02/25/adventures-with-json-and-silverlight-and-the-new-york-times/</guid>
		<description><![CDATA[Summary: In this post, I walk through the basics of using Silverlight to query the New York Times Article API and display the results of the query. You can see the final result below. You can also download source code for this project here. JSON/Silverlight/New York Times project files Huge thanks to Josh Holmes, whose [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Summary:</strong> In this post, I walk through the basics of using Silverlight to query the New York Times Article API and display the results of the query. You can see the final result below.</p>
<p><iframe src="http://silverlight.services.live.com/invoke/77530/JSON_NYT_Tutorial_Part_1/iframe.html" scrolling="no" frameborder="0" style="width:400px; height:300px"></iframe></p>
<p>You can also download source code for this project here.</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/02/jsonnewyorktimestutorial_part_1.zip">JSON/Silverlight/New York Times project files </a></p>
<p>Huge thanks to Josh Holmes, whose <a href="http://www.joshholmes.com/blog/2009/01/20/PlayingWithJSON.aspx">JSON/Silverlight tutorial</a> was the base of much of this project.</p>
<p>This project is somewhat code-intensive (and kind of long, expect 30-60 minutes), so if you just want the utility provided here without any of the work, you can skip over to <a href="http://www.designersilverlight.com/2009/02/25/using-silverlight-to-display-json-data-collected-from-the-new-york-times-api/">my post on displaying the results</a>, which is strictly a Blend tutorial.</p>
<p>However, I recommend walking through this one since it will help get you to a point of pulling real data from an API, which I’ve found to be a wonderful help as I practice putting together data-based designs. One of the things I&#8217;ve been wanting to be able to do as a designer is to explore a data set easily and quickly so that I can have data to play with in my interfaces. I found exactly what I wanted in the New York Times API, but then I found out I had to learn JSON.</p>
<p>&#8220;Oh great,&#8221; I said to myself, &#8220;another technology for me to learn.&#8221; But it turned out that I didn&#8217;t actually have to learn that much, because Visual Studio does nearly all the work for me.</p>
<h3>Super Quick Introduction to JSON</h3>
<p>If you&#8217;re not interested and you want to get to the business of grabbing New York Times data, you can skip it . It is helpful, but not strictly necessary.</p>
<p>JSON stands for JavaScript Object Notation and is basically just a really handy way to pass data along in a web service. It is very simple&#8230; within a set of curly brackets, you will have name/value pairs separated by a colon with each piece of data.<br />
Example:<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;FirstName : &#8220;Matthias&#8221;,<br />
&nbsp;&nbsp;&nbsp;&nbsp;LastName : &#8220;Shapiro&#8221;,<br />
&nbsp;&nbsp;&nbsp;&nbsp;Blog : &#8220;Designer WPF&#8221;<br />
}</p>
<p>This is a JSON object. Arrays are created by using square brackets and JSON obejcts can be placed into a Javascript var. These things are not really related in anyway, but putting them in the same sentence allows me to only write one more example instead of two</p>
<p>Example:<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;FirstName: “Matthias”,<br />
&nbsp;&nbsp;&nbsp;&nbsp;LastName: “Shapiro”,<br />
&nbsp;&nbsp;&nbsp;&nbsp;Siblings : [<br />
&nbsp;&nbsp;&nbsp;&nbsp;{Name : “Abby”},<br />
&nbsp;&nbsp;&nbsp;&nbsp;{Name : “Joel”},<br />
&nbsp;&nbsp;&nbsp;&nbsp;{Name : “Anna”},<br />
&nbsp;&nbsp;&nbsp;&nbsp;{Name : “John”},<br />
&nbsp;&nbsp;&nbsp;&nbsp;{Name : “Nate”}<br />
&nbsp;&nbsp;&nbsp;&nbsp;]<br />
}</p>
<p>And there we have the basics of JSON.</p>
<h3>End of Super Quick Introduction to JSON</h3>
<p>What is so awesome about JSON and Silverlight is that Visual Studio 2008 has a set of JSON-friendly classes that make working with JSON a breeze. Which is really handy because the New York Times, which is a dream come true for the new data-gatherer, delivers JSON results. So let’s walk through making a call to the New York Times Article API, handling the data we get back, and putting it into a Listbox for viewing.</p>
<p>First, <a href="http://developer.nytimes.com/">go to the New York Times Developer site</a>, log in (or register) and get your API key.</p>
<p>Next, start a new Silverlight project in Visual Studio 2008. I named mine “JSONNewYorkTimesTutorial”.</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/02/clip-image0019.png"><img style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" title="clip_image001[9]" src="http://www.designersilverlight.com/wp-content/uploads/2009/02/clip-image0019-thumb.png" border="0" alt="clip_image001[9]" width="659" height="424" /></a></p>
<p>Open your new project in Blend, pull up Page.xaml and add a TextBlock, ListBox, a TextBox and a Button. Name the ListBox “ResultsDisplay” and the TextBox “SearchText”.</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/02/clip-image00113.png"><img style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" title="clip_image001[13]" src="http://www.designersilverlight.com/wp-content/uploads/2009/02/clip-image00113-thumb.png" border="0" alt="clip_image001[13]" width="299" height="105" /></a></p>
<p>Now, my Page.xaml looks like this.</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/02/clip-image00111.png"><img style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" title="clip_image001[11]" src="http://www.designersilverlight.com/wp-content/uploads/2009/02/clip-image00111-thumb.png" border="0" alt="clip_image001[11]" width="457" height="346" /></a></p>
<p>OK… now let’s go to the code-behind for our project go to the event section of the button in Blend and type “PerformQuery” into the “Click” event.</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/02/clip-image00115.png"><img style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" title="clip_image001[15]" src="http://www.designersilverlight.com/wp-content/uploads/2009/02/clip-image00115-thumb.png" border="0" alt="clip_image001[15]" width="264" height="104" /></a></p>
<p>This will automatically insert the necessary code into the code-behind, so pull up Visual Studio. Before we implement a call to the NYT API, lets add some useful stuff. If you have not yet gone to <a href="http://developer.nytimes.com/apps/register?authChecked=1">get your Developer key, do so now</a>.</p>
<p><span style="color: blue">private string </span>myApiKey;<br />
<span style="color: blue">private </span><span style="color: #2b91af">WebClient </span>callNYT;</p>
<p><span style="color: blue">public </span>Page()<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;InitializeComponent();<br />
&nbsp;&nbsp;&nbsp;&nbsp;myApiKey = <span style="color: #a31515">&#8220;&amp;api-key=(put your api key here. No, you may not have mine)&#8221;</span>;<br />
&nbsp;&nbsp;&nbsp;&nbsp;callNYT = <span style="color: blue">new </span><span style="color: #2b91af">WebClient</span>();<br />
&nbsp;&nbsp;&nbsp;&nbsp;callNYT.OpenReadCompleted += <span style="color: blue">new </span><span style="color: #2b91af">OpenReadCompletedEventHandler</span>(callNYT_OpenReadCompleted);<br />
}</p>
<p>In the code above, we’ve created a string that we can use to apply our unique NYT API key and we’ve created an instance of WebClient to call and receive information from the NYT API. When an object from the NYT API has been received , it will call the OpenReadCompleted event, which will be handled by our callNYT_OpenReadCompleted method.</p>
<p>(By the way, if you’re not getting the proper intellisense for the “Web Client” part, add “<span style="color: blue">using </span>System.Net;&#8221; to the top of your file.)</p>
<p>Now on to our button method. There are tons of things we can add to our query to find the exact information we want. But in the interest of simplicity, this post will deal only with a simple text search. To that end, let’s go to our PerformQuery method and turn it into this:</p>
<p><span style="color: blue">private void </span>PerformQuery(<span style="color: blue">object </span>sender, <span style="color: #2b91af">RoutedEventArgs </span>e)<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">string </span>NYTQueryBase = <span style="color: #a31515">&#8220;http://api.nytimes.com/svc/search/v1/article&#8221;</span>;<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">string </span>SearchTerm = <span style="color: #a31515">&#8220;?query=&#8221;</span>+ SearchText.Text;<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af">Uri </span>queryUri = <span style="color: blue">new </span><span style="color: #2b91af">Uri</span>(NYTQueryBase + SearchTerm + myApiKey);</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;callNYT.OpenReadAsync(queryUri);<br />
}</p>
<p>We’ve done two things here. The first is that we built our search query using the query base, the query string and our API key. That was simple enough.</p>
<p>Next, we’ve going to use the WebClient we created to call our new query. When the query has been completed, our program will run the callNYT_OpenReadCompleted method with its result, which will be a JSON object constructed by the NYT servers. We will get back a JSON object with the following:</p>
<ul>
<li>offset – We will get 10 results per page. The offset tells us which page of the results we’re on. The default is 0, which gives us results 0 – 9.</li>
<li>tokens – this is a array of our search terms.</li>
<li>total – this is an integer indicating of how many results there were for our search.</li>
<li>results – this is an array of results with the following format
<ul>
<li>body – a portion of the beginning of the article</li>
<li>byline – the article byline, usually including the author name</li>
<li>date – the date the article appeared, in a “yyyymmdd” format. For example, today would be “20090225”.</li>
<li>title – the article title</li>
<li>url – a url link to the article</li>
</ul>
</li>
</ul>
<p>A quick note: The NYT API is extremely flexible and we can actually define how we want our results to come back. This is just the default result format for the purposes of demonstration.</p>
<p>Before we handle this object, we want to create a class for the results. Right click on your project and go to “Add –&gt; Class…”. Name your new class “NYTResult.cs” and make sure it looks like this:</p>
<p><span style="color: blue">public class </span><span style="color: #2b91af">NYTResult </span>{<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">public string </span>Body { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">public string </span>Byline { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">public </span><span style="color: #2b91af">DateTime </span>Date { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">public string </span>Title { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">public </span><span style="color: #2b91af">Uri </span>Url { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }<br />
}</p>
<p>I added the following method to the class to handle the date conversion from the NYT format to a .NET DateTime object.</p>
<p><span style="color: blue">public </span><span style="color: #2b91af">DateTime </span>formattedDateTime(<span style="color: blue">string </span>NYT_Time)<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue"><span style="color: #444444;"> </span>int </span>year = <span style="color: #2b91af">Convert</span>.ToInt32(NYT_Time.Substring(0, 4));<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">int </span>month = <span style="color: #2b91af">Convert</span>.ToInt32(NYT_Time.Substring(4, 2));<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">int </span>day = <span style="color: #2b91af">Convert</span>.ToInt32(NYT_Time.Substring(6, 2));<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af">DateTime </span>finalDateTime = <span style="color: blue">new </span><span style="color: #2b91af">DateTime</span>(year, month, day);<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">return </span>finalDateTime;<br />
}</p>
<p>OK… now we’re really ready to handle the JSON object. Right click on the references in your project and select “Add Reference…”</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/02/clip-image001.png"><img style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" title="clip_image001" src="http://www.designersilverlight.com/wp-content/uploads/2009/02/clip-image001-thumb.png" border="0" alt="clip_image001" width="287" height="81" /></a></p>
<p>In  your “Add References” box, select “System.Json” and click “OK”.</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/02/clip-image0015.png"><img style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" title="clip_image001[5]" src="http://www.designersilverlight.com/wp-content/uploads/2009/02/clip-image0015-thumb.png" border="0" alt="clip_image001[5]" width="481" height="408" /></a></p>
<p>Add “<span style="color: blue">using </span>System.Json;” to the references in your Page.xaml.cs file. And, just for good measure, add “<span style="color: blue">using </span>System.Collections.ObjectModel;” as well.</p>
<p>Go to the callNYT_OpenReadCompleted method and enter the following. I’ve tried to comment the code so that I don’t need to further explain it. Side note: I’m not always the best at understanding what is self-explanatory and what I need to elaborate on. If there are any additional questions, post them in the comments and I’ll answer as quickly as I can.</p>
<p><span style="color: blue">void </span>callNYT_OpenReadCompleted(<span style="color: blue">object </span>sender, <span style="color: #2b91af">OpenReadCompletedEventArgs </span>e)<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: green">//grab our result and make a JSON Object out of it<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span> <span style="color: green">//    then extract the results array from that object<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span> <span style="color: #2b91af">JsonObject </span>completeResult = (<span style="color: #2b91af">JsonObject</span>)<span style="color: #2b91af">JsonObject</span>.Load(e.Result);<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af">JsonArray </span>resultsArray = (<span style="color: #2b91af">JsonArray</span>)completeResult[<span style="color: #a31515">"results"</span>];</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: green">//an observable collection to hold the data and attach it to our ListBox<br />
</span><br />
&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">ObservableCollection</span>&lt;<span style="color: #2b91af">NYTResult</span>&gt; resultCollection = <span style="color: blue">new </span><span style="color: #2b91af">ObservableCollection</span>&lt;<span style="color: #2b91af">NYTResult</span>&gt;();</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: green">//iterate through the results and transfer the data from a<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span> <span style="color: green">//   JSON object into our nice pretty .NET object<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span> <span style="color: blue">foreach </span>(<span style="color: #2b91af">JsonObject </span>NYTRawResult <span style="color: blue">in </span>resultsArray)<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af">NYTResult </span>singleResult = <span style="color: blue">new </span><span style="color: #2b91af">NYTResult</span>();</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: green">//don&#8217;t forget to check your results&#8230; an article might not have a<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span> <span style="color: green">//  byline or a link<br />
</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">if </span>(NYTRawResult.Keys.Contains(<span style="color: #a31515">&#8220;body&#8221;</span>))<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;singleResult.Body = NYTRawResult[<span style="color: #a31515">"body"</span>];<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">if</span>(NYTRawResult.Keys.Contains(<span style="color: #a31515">&#8220;byline&#8221;</span>))<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;singleResult.Byline = NYTRawResult[<span style="color: #a31515">"byline"</span>];<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">if </span>(NYTRawResult.Keys.Contains(<span style="color: #a31515">&#8220;date&#8221;</span>))<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;singleResult.Date = singleResult.formattedDateTime(NYTRawResult[<span style="color: #a31515">"date"</span>]);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">if </span>(NYTRawResult.Keys.Contains(<span style="color: #a31515">&#8220;title&#8221;</span>))<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;singleResult.Title = NYTRawResult[<span style="color: #a31515">"title"</span>];<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">if </span>(NYTRawResult.Keys.Contains(<span style="color: #a31515">&#8220;url&#8221;</span>))<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;singleResult.Url = <span style="color: blue">new </span><span style="color: #2b91af">Uri</span>(NYTRawResult[<span style="color: #a31515">"url"</span>]);</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: green">//add our new result to the collection<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span> resultCollection.Add(singleResult);<br />
&nbsp;&nbsp;&nbsp;&nbsp;}</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: green">//assign the result as the source for our ListBox<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span> ResultsDisplay.ItemsSource = resultCollection;</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: green">//take the overall article count and display it<br />
</span>&nbsp;&nbsp;&nbsp;&nbsp; resultCount.Text = <span style="color: #a31515">&#8220;Number of articles: &#8221; </span>+ completeResult[<span style="color: #a31515">"total"</span>].ToString();<br />
}</p>
<p>Now, we can run our project. Type something into the TextBox and hit the button and we get this:<br />
<a href="http://www.designersilverlight.com/wp-content/uploads/2009/02/clip-image0017.png"><img style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" title="clip_image001[7]" src="http://www.designersilverlight.com/wp-content/uploads/2009/02/clip-image0017-thumb.png" border="0" alt="clip_image001[7]" width="404" height="303" /></a></p>
<p>Not exactly the most readable thing ever. So let’s add the following to the ListBox XAML:</p>
<p><span style="color: red">DisplayMemberPath</span><span style="color: blue">=&#8221;Title&#8221;</span></p>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Now we get something a little more like this (go ahead and give it a whirl):</p>
<p><iframe src="http://silverlight.services.live.com/invoke/77530/JSON_NYT_Tutorial_Part_1/iframe.html" scrolling="no" frameborder="0" style="width:400px; height:300px"></iframe></p>
<p>Much better. Remember, this is a simple query, so it’s only looking for items that have that word in the article… it might not be in the title.</p>
<p><a href="http://www.designersilverlight.com/2009/02/25/using-silverlight-to-display-json-data-collected-from-the-new-york-times-api/">My next post builds on this one</a> and I walk through building a more useful display for our results. It will be far less code intensive and far more designer centric.</p>
<p>I’ve made the source available for this project. I’ve taken out my NYT API key, so it will not run unless you get your own and put it in.</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/02/jsonnewyorktimestutorial_part_1.zip">JSON &#8211; Silverlight &#8211; New York Times Tutorial Part 1 project files</a></p>
<iframe src="http://www.facebook.com/plugins/like.php?href=http://www.designersilverlight.com/2009/02/25/adventures-with-json-and-silverlight-and-the-new-york-times/&amp;layout=standard&amp;show_faces=1&amp;width=450&amp;action=like&amp;colorscheme=light&amp;font=" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:25px"></iframe><div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Adventures+with+JSON+and+Silverlight+%28and+the+New+York+Times%29+http%3A%2F%2Fdesignersilverlight.com%2F%3Fp%3D550" title="Post to Twitter"><img class="nothumb" src="http://www.designersilverlight.com/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter.png" alt="Post to Twitter" /></a> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Adventures+with+JSON+and+Silverlight+%28and+the+New+York+Times%29+http%3A%2F%2Fdesignersilverlight.com%2F%3Fp%3D550" title="Post to Twitter">Tweet This Post</a></p></div>]]></content:encoded>
			<wfw:commentRss>http://www.designersilverlight.com/2009/02/25/adventures-with-json-and-silverlight-and-the-new-york-times/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

