<?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>vijayjoshi.org &#187; JSON</title>
	<atom:link href="http://www.vijayjoshi.org/category/json/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.vijayjoshi.org</link>
	<description>php &#124; javascript &#124; ajax &#124; and all things web</description>
	<lastBuildDate>Sun, 20 Nov 2011 15:24:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>PHP class for expanding and shortening URLs using Google URL shortener API</title>
		<link>http://www.vijayjoshi.org/2011/01/21/php-class-for-expanding-and-shortening-urls-using-google-url-shortener-api/</link>
		<comments>http://www.vijayjoshi.org/2011/01/21/php-class-for-expanding-and-shortening-urls-using-google-url-shortener-api/#comments</comments>
		<pubDate>Thu, 20 Jan 2011 19:55:14 +0000</pubDate>
		<dc:creator>Vijay Joshi</dc:creator>
				<category><![CDATA[JSON]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Google]]></category>

		<guid isPermaLink="false">http://www.vijayjoshi.org/?p=1156</guid>
		<description><![CDATA[
			
				
			
		
I have combined the 2 previous posts for expanding and shortening goo.gl URLs and have created a small class for it. Below is the download link for the class.

Expanding a URL
&#60;?php
include('GAPIClass.php');

$objAPI = new GAPIClass('your_api_key_here');
$result = $objAPI-&#62;expand('http://goo.gl/WlFX');
if(FALSE === $result)
{
	echo $objAPI-&#62;error;
}
else
{
	echo 'Long URL:'.$result;
}
?&#62;
Shorten A URL
&#60;?php
include('GAPIClass.php');

$objAPI = new GAPIClass('your_api_key_here');
$result = $objAPI-&#62;shorten('https://www.packtpub.com/php-jquery-cookbook-to-create-interactive-web-applications/book');

if(FALSE === $result)
{
	echo $objAPI-&#62;error;
}
else
{
	echo 'Short URL:'.$result;
}
?&#62;
As I have mentioned earlier that Google recommends ...]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop --><!-- End Shareaholic LikeButtonSetTop --><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.vijayjoshi.org%2F2011%2F01%2F21%2Fphp-class-for-expanding-and-shortening-urls-using-google-url-shortener-api%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.vijayjoshi.org%2F2011%2F01%2F21%2Fphp-class-for-expanding-and-shortening-urls-using-google-url-shortener-api%2F&amp;source=v08i&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>I have combined the 2 previous posts for <a href="http://www.vijayjoshi.org/2011/01/12/using-php-and-curl-to-expand-urls-using-google-url-shortener-api/">expanding </a>and <a href="http://www.vijayjoshi.org/2011/01/12/php-shorten-urls-using-google-url-shortener-api/">shortening</a> goo.gl URLs and have created a small class for it. Below is the download link for the class.</p>
<p><a href="http://www.vijayjoshi.org/examples/GAPIClass.php.txt"><img class="aligncenter size-full wp-image-451" title="Download GAPIClass" src="http://www.vijayjoshi.org/wp-content/uploads/download.png" alt="Download GAPIClass" width="180" height="45" /></a></p>
<h4><span style="text-decoration: underline;">Expanding a URL</span></h4>
<pre class="brush:php">&lt;?php
include('GAPIClass.php');

$objAPI = new GAPIClass('your_api_key_here');
$result = $objAPI-&gt;expand('http://goo.gl/WlFX');
if(FALSE === $result)
{
	echo $objAPI-&gt;error;
}
else
{
	echo 'Long URL:'.$result;
}
?&gt;</pre>
<h4><span style="text-decoration: underline;">Shorten A URL</span></h4>
<pre class="brush:php">&lt;?php
include('GAPIClass.php');

$objAPI = new GAPIClass('your_api_key_here');
$result = $objAPI-&gt;shorten('https://www.packtpub.com/php-jquery-cookbook-to-create-interactive-web-applications/book');

if(FALSE === $result)
{
	echo $objAPI-&gt;error;
}
else
{
	echo 'Short URL:'.$result;
}
?&gt;</pre>
<p>As I have mentioned earlier that Google recommends an API key. The class has a flag variable called <em><strong>keyWarning</strong></em>. By default it is true and it will show the following warning message if you do not pass an API key while instantiating the class:</p>
<blockquote><p>Currently you are not using an API key. It is recommended that you use one. <a href="http://code.google.com/apis/urlshortener/v1/authentication.html#key">Click here to learn more about the API key</a></p></blockquote>
<p>To turn this notification off, just use this line:</p>
<pre class="brush:php">$objAPI-&gt;keyWarning = false;</pre>
<p><a href="http://www.vijayjoshi.org/examples/GAPIClass.php.txt"><img class="aligncenter size-full wp-image-451" title="Download GAPIClass" src="http://www.vijayjoshi.org/wp-content/uploads/download.png" alt="Download GAPIClass" width="180" height="45" /></a></p>
<div class="shr-publisher-1156"></div><!-- Start Shareaholic LikeButtonSetBottom --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:right;height:30px;'><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.vijayjoshi.org%2F2011%2F01%2F21%2Fphp-class-for-expanding-and-shortening-urls-using-google-url-shortener-api%2F' data-shr_title='PHP+class+for+expanding+and+shortening+URLs+using+Google+URL+shortener+API'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom -->]]></content:encoded>
			<wfw:commentRss>http://www.vijayjoshi.org/2011/01/21/php-class-for-expanding-and-shortening-urls-using-google-url-shortener-api/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP: Shorten URLs using Google URL shortener API</title>
		<link>http://www.vijayjoshi.org/2011/01/12/php-shorten-urls-using-google-url-shortener-api/</link>
		<comments>http://www.vijayjoshi.org/2011/01/12/php-shorten-urls-using-google-url-shortener-api/#comments</comments>
		<pubDate>Wed, 12 Jan 2011 06:27:42 +0000</pubDate>
		<dc:creator>Vijay Joshi</dc:creator>
				<category><![CDATA[JSON]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Google]]></category>

		<guid isPermaLink="false">http://www.vijayjoshi.org/?p=1093</guid>
		<description><![CDATA[
			
				
			
		
In past 2 posts you saw how short URLs can be expanded using the Google URL shortener API. This post shows how URLs can be shortened using this API.
API key is not mandatory for test purpose. Use this link to get an API key.
For shortening URLs, 2 points must be noted. First, a HTTP POST is required and the post data ...]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop --><!-- End Shareaholic LikeButtonSetTop --><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.vijayjoshi.org%2F2011%2F01%2F12%2Fphp-shorten-urls-using-google-url-shortener-api%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.vijayjoshi.org%2F2011%2F01%2F12%2Fphp-shorten-urls-using-google-url-shortener-api%2F&amp;source=v08i&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>In past 2 posts you saw how short URLs can be expanded using the Google URL shortener API. This post shows how URLs can be shortened using this API.</p>
<p>API key is not mandatory for test purpose. <a title="Get API Key" href="http://code.google.com/apis/console/">Use this link to get an API key.</a></p>
<p><a title="Get API Key" href="http://code.google.com/apis/console/"></a>For shortening URLs, 2 points must be noted. First, a HTTP POST is required and the post data should be in JSON format. Here is the code:</p>
<pre class="brush:php">//This is the URL you want to shorten
$longUrl = 'https://www.packtpub.com/php-jquery-cookbook-to-create-interactive-web-applications/book';
$apiKey = 'your_api_key_here';
//Get API key from : http://code.google.com/apis/console/

$postData = array('longUrl' =&gt; $longUrl, 'key' =&gt; $apiKey);
$jsonData = json_encode($postData);

$curlObj = curl_init();

curl_setopt($curlObj, CURLOPT_URL, 'https://www.googleapis.com/urlshortener/v1/url');
curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curlObj, CURLOPT_HEADER, 0);
curl_setopt($curlObj, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
curl_setopt($curlObj, CURLOPT_POST, 1);
curl_setopt($curlObj, CURLOPT_POSTFIELDS, $jsonData);

$response = curl_exec($curlObj);

//change the response json string to object
$json = json_decode($response);

curl_close($curlObj);

echo 'Shortened URL is: '.$json-&gt;id;</pre>
<p>Here is the JSON response from Google:</p>
<pre class="brush:js">{
 "kind": "urlshortener#url",
 "id": "http://goo.gl/lBfOH",
 "longUrl": "https://www.packtpub.com/php-jquery-cookbook-to-create-interactive-web-applications/book"
}</pre>
<p>You can also read these:</p>
<ul>
<li><a href="http://www.vijayjoshi.org/2011/01/21/php-class-for-expanding-and-shortening-urls-using-google-url-shortener-api/">PHP class for expanding and shortening URLs using Google URL shortener API</a></li>
<li><a href="http://www.vijayjoshi.org/2011/01/12/using-php-and-curl-to-expand-urls-using-google-url-shortener-api/">Expanding URLs using cURL</a></li>
<li><a href="http://www.vijayjoshi.org/2011/01/11/php-expanding-urls-using-google-url-shortener-api/">Expand URLs using file_get_contents</a></li>
</ul>
<div class="shr-publisher-1093"></div><!-- Start Shareaholic LikeButtonSetBottom --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:right;height:30px;'><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.vijayjoshi.org%2F2011%2F01%2F12%2Fphp-shorten-urls-using-google-url-shortener-api%2F' data-shr_title='PHP%3A+Shorten+URLs+using+Google+URL+shortener+API'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom -->]]></content:encoded>
			<wfw:commentRss>http://www.vijayjoshi.org/2011/01/12/php-shorten-urls-using-google-url-shortener-api/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>Using PHP and cURL to expand URLs using Google URL shortener API</title>
		<link>http://www.vijayjoshi.org/2011/01/12/using-php-and-curl-to-expand-urls-using-google-url-shortener-api/</link>
		<comments>http://www.vijayjoshi.org/2011/01/12/using-php-and-curl-to-expand-urls-using-google-url-shortener-api/#comments</comments>
		<pubDate>Tue, 11 Jan 2011 18:49:03 +0000</pubDate>
		<dc:creator>Vijay Joshi</dc:creator>
				<category><![CDATA[JSON]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Google]]></category>

		<guid isPermaLink="false">http://www.vijayjoshi.org/?p=1082</guid>
		<description><![CDATA[
			
				
			
		
Yesterday I wrote this post explaining how short goo.gl URLs can be expanded using the newly launched goo.gl API. In that code we used php function get_file_contents to get the response from API.
How about implementing the same functionality using cURL now? If you have libcurl support installed with PHP, you are ready to use the cURL functions of PHP.
Here is ...]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop --><!-- End Shareaholic LikeButtonSetTop --><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.vijayjoshi.org%2F2011%2F01%2F12%2Fusing-php-and-curl-to-expand-urls-using-google-url-shortener-api%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.vijayjoshi.org%2F2011%2F01%2F12%2Fusing-php-and-curl-to-expand-urls-using-google-url-shortener-api%2F&amp;source=v08i&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Yesterday I wrote <a href="http://www.vijayjoshi.org/2011/01/11/php-expanding-urls-using-google-url-shortener-api/">this post</a> explaining how short <a title="Google URL shortener" href="http://goo.gl">goo.gl</a> URLs can be expanded using the newly launched goo.gl API. In that code we used php function <strong><em>get_file_contents</em></strong> to get the response from API.</p>
<p>How about implementing the same functionality using cURL now? If you have<strong> libcurl</strong> support installed with PHP, you are ready to use the cURL functions of PHP.</p>
<p>Here is the code. API key is not mandatory but Google recommends that you use one. It will increase your per day usage limits too. <a title="Google API key" href="http://code.google.com/apis/console/">Follow this link</a> to get an API key.</p>
<pre class="brush:php">$shortUrl = 'http://goo.gl/WlFX';
$apiKey = 'your_api_key_here';

$curlObj = curl_init();

curl_setopt($curlObj, CURLOPT_URL, 'https://www.googleapis.com/urlshortener/v1/url?shortUrl='.$shortUrl.'&amp;key='.$apiKey);
curl_setopt($curlObj, CURLOPT_HEADER, 0);
curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, 0);

$response = curl_exec($curlObj);

curl_close($curlObj);
$json = json_decode($response);

if($json-&gt;status === "OK")
{
	echo $json-&gt;longUrl;
}
else
{
	echo 'Bad luck.';
}</pre>
<p>And here is the full JSON response:</p>
<pre class="brush:js">{
 "kind": "urlshortener#url",
 "id": "http://goo.gl/WlFX",
 "longUrl": "https://www.packtpub.com/php-jquery-cookbook-to-create-interactive-web-applications/book",
 "status": "OK"
}</pre>
<p>Am important point to note. Since the API is on https, set the value for <strong><em>CURLOPT_SSL_VERIFYPEER</em></strong> to false. This will stop cURL from verifying the SSL certificate.</p>
<div class="shr-publisher-1082"></div><!-- Start Shareaholic LikeButtonSetBottom --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:right;height:30px;'><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.vijayjoshi.org%2F2011%2F01%2F12%2Fusing-php-and-curl-to-expand-urls-using-google-url-shortener-api%2F' data-shr_title='Using+PHP+and+cURL+to+expand+URLs+using+Google+URL+shortener+API'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom -->]]></content:encoded>
			<wfw:commentRss>http://www.vijayjoshi.org/2011/01/12/using-php-and-curl-to-expand-urls-using-google-url-shortener-api/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>PHP: Expanding URLs using Google URL shortener API</title>
		<link>http://www.vijayjoshi.org/2011/01/11/php-expanding-urls-using-google-url-shortener-api/</link>
		<comments>http://www.vijayjoshi.org/2011/01/11/php-expanding-urls-using-google-url-shortener-api/#comments</comments>
		<pubDate>Tue, 11 Jan 2011 08:22:12 +0000</pubDate>
		<dc:creator>Vijay Joshi</dc:creator>
				<category><![CDATA[JSON]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Google]]></category>

		<guid isPermaLink="false">http://www.vijayjoshi.org/?p=1073</guid>
		<description><![CDATA[
			
				
			
		
Yesterday. google launched an API for its URL shortening service http://goo.gl. Using this API developers will be able to expand/shorten URLs and get a user&#8217;s analytics and history programmaticaly.
Below is a small snippet of PHP code that expands a short URL. The response comes in JSON format, so we will use the json_decode function to convert it to object.
$shortUrl = 'http://goo.gl/WlFX';
$response ...]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop --><!-- End Shareaholic LikeButtonSetTop --><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.vijayjoshi.org%2F2011%2F01%2F11%2Fphp-expanding-urls-using-google-url-shortener-api%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.vijayjoshi.org%2F2011%2F01%2F11%2Fphp-expanding-urls-using-google-url-shortener-api%2F&amp;source=v08i&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Yesterday. google <a title="Google URL shortener API" href="http://googlecode.blogspot.com/2011/01/google-url-shortener-gets-api.html">launched an API</a> for its URL shortening service <a href="http://goo.gl">http://goo.gl</a>. Using this API developers will be able to expand/shorten URLs and get a user&#8217;s analytics and history programmaticaly.</p>
<p>Below is a small snippet of PHP code that expands a short URL. The response comes in JSON format, so we will use the <strong><em>json_decode</em></strong> function to convert it to object.</p>
<pre class="brush:php">$shortUrl = 'http://goo.gl/WlFX';
$response = file_get_contents('https://www.googleapis.com/urlshortener/v1/url?shortUrl='.$shortUrl);
$json = json_decode($response);

if($json-&gt;status === "OK")
{
	echo $json-&gt;longUrl;
}
else
{
	echo 'Bad luck.';
}</pre>
<p>Here is the full successfull response in JSON format.</p>
<pre class="brush:js">{
 "kind": "urlshortener#url",
 "id": "http://goo.gl/WlFX",
 "longUrl": "https://www.packtpub.com/php-jquery-cookbook-to-create-interactive-web-applications/book",
 "status": "OK"
}</pre>
<p>Finally, note that Google recommends the use of an API key. You can <a title="Google API Key" href="http://code.google.com/apis/console-help/#UsingKeys">visit this link</a> to read more about key and get a key from <a href="http://code.google.com/apis/console/">this link</a>.</p>
<p>After using the key, your URL will look like the following:</p>
<pre class="brush:php">$shortUrl = 'http://goo.gl/WlFX';
$apiKey = 'your_key_here';
$response = file_get_contents('https://www.googleapis.com/urlshortener/v1/url?shortUrl='.$shortUrl.'&amp;key='.$apiKey);</pre>
<div class="shr-publisher-1073"></div><!-- Start Shareaholic LikeButtonSetBottom --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:right;height:30px;'><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.vijayjoshi.org%2F2011%2F01%2F11%2Fphp-expanding-urls-using-google-url-shortener-api%2F' data-shr_title='PHP%3A+Expanding+URLs+using+Google+URL+shortener+API'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom -->]]></content:encoded>
			<wfw:commentRss>http://www.vijayjoshi.org/2011/01/11/php-expanding-urls-using-google-url-shortener-api/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Twitter API and jQuery to display recent tweets</title>
		<link>http://www.vijayjoshi.org/2010/12/24/using-twitter-api-and-jquery-to-display-recent-tweets/</link>
		<comments>http://www.vijayjoshi.org/2010/12/24/using-twitter-api-and-jquery-to-display-recent-tweets/#comments</comments>
		<pubDate>Fri, 24 Dec 2010 06:52:13 +0000</pubDate>
		<dc:creator>Vijay Joshi</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[JSON]]></category>

		<guid isPermaLink="false">http://www.vijayjoshi.org/?p=886</guid>
		<description><![CDATA[
			
				
			
		
In this article I will show you how you can display your (or any other user&#8217;s) recent tweets with some jQuery magic. We will create a widget (sort of) that will fetch n number of latest tweets from public timeline of a user using Twitter API and then we will display them in a nice list.
End result will look like ...]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop --><!-- End Shareaholic LikeButtonSetTop --><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.vijayjoshi.org%2F2010%2F12%2F24%2Fusing-twitter-api-and-jquery-to-display-recent-tweets%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.vijayjoshi.org%2F2010%2F12%2F24%2Fusing-twitter-api-and-jquery-to-display-recent-tweets%2F&amp;source=v08i&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>In this article I will show you how you can display your (or any other user&#8217;s) recent tweets with some jQuery magic. We will create a widget (sort of) that will fetch n number of latest tweets from public timeline of a user using Twitter API and then we will display them in a nice list.</p>
<p>End result will look like the following.</p>
<p style="text-align: left;"><img class="aligncenter size-full wp-image-887" title="tweets" src="http://www.vijayjoshi.org/wp-content/uploads/tweets.png" alt="tweets" width="480" height="388" /></p>
<p style="text-align: left;">Have a look at a live example.</p>
<p style="text-align: center;"><a href="http://vijayjoshi.org/examples/twitter-latest-tweets.html"><img class="size-full wp-image-453 aligncenter" title="View Demo" src="http://www.vijayjoshi.org/wp-content/uploads/demo.png" alt="View Demo" width="180" height="45" /></a></p>
<p style="text-align: left;">Let us start creating it now. First write some HTML. Define some css styles in the head section which will control the display of list. In the body section, there is a select box with id <strong><em>layout</em></strong> which will be used to select either a wide or a narrow layout for displaying the tweets. Then create a <strong><em>div </em></strong>with id <strong>tweets</strong>. We will create a <strong><em>ul</em></strong> inside this div which will have tweets as list items.</p>
<p style="text-align: left;"><span id="more-886"></span></p>
<p style="text-align: left;">
<p style="text-align: left;">
<pre class="brush:xml">&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
	&lt;head&gt;
		&lt;title&gt;Latest Tweets Widget - Vijay Joshi | http://vijayjoshi.org&lt;/title&gt;
		&lt;style type="text/css"&gt;
			body{ margin:0;font-family:verdana,arial;font-size:13px;width:100%;}
			p{margin:0;padding:5px;}
			span{font-size: 10px; font-weight: bold;}
			#container{margin:0 auto;padding:5px;width:600px;}
			#tweets{background-color:#ACAAFC;}
			#tweets ul, li{list-style:none;margin:0;padding:5px;}
			#tweets h2{margin: 0pt; padding:5px;}
			#tweets a { color: #FF0000;}
			.even{ border-bottom: 1px dotted #000;color:#343234;background-color:#FCFEFC;}
			.odd{border-bottom: 1px dotted #000;color:#343234;background-color:#DCDEFC;}
			.clear{	clear:both;	}
		&lt;/style&gt;
	&lt;/head&gt;
	&lt;body&gt;
		&lt;div id="container"&gt;
			&lt;strong&gt;Select layout&lt;/strong&gt;
			&lt;select id="layout"&gt;
				&lt;option value="600"&gt;Wide&lt;/option&gt;
				&lt;option value="300"&gt;Narrow&lt;/option&gt;
			&lt;/select&gt;
			&lt;div class="clear"&gt; &lt;/div&gt;
			&lt;div id="tweets"&gt;&lt;/div&gt;
		&lt;/div&gt;
	&lt;/body&gt;
&lt;/html&gt;</pre>
<p style="text-align: left;">
<p style="text-align: left;"><strong>jQuery Code</strong></p>
<p style="text-align: left;">Just before the body tag closes, add path to jQuery library either from your local file system or the Google CDN.</p>
<p style="text-align: left;">
<pre class="brush:js">&lt;script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"&gt;&lt;/script&gt;</pre>
<p>With this we are ready to pull the tweets. Start by defining 2 variables <strong><em>userName</em></strong> and <strong><em>count</em></strong>. Inside document.ready add a change event handler to select box with id <strong>layout</strong>. It will change its width from 600px to 300px on selecting narrow and again back to 600px on selecting wide.</p>
<p>Now call a function <strong>getTweets </strong>and pass user name and number of tweets to it.</p>
<pre class="brush:js">var userName = 'v08i';
var count = 8;
$(document).ready(function()
{
	$('#layout').change(function()
	{
		$('#container').css({ 'width' : $(this).val()+ 'px'});
	});
	getTweets(userName, count);

});</pre>
<p>getTweets function will actually contact the Twitter API and will fetch tweets from there. We will request the response in JSON format from Twitter using jQuery&#8217;s <strong>getJSON </strong>method.</p>
<p>Before we proceed let us talk a bit technical. As you probably know, browsers do not allow cross-domain ajax requests due to security reasons. In this case the example we are creating is at domain <span style="text-decoration: underline;">vijayjoshi.org</span> while the twitter domain is <span style="text-decoration: underline;">api.twitter.com</span>. If both domains were same, a simple AJAX request would have sufficed. To overcome this problem JSONP aka &#8220;JSON with Padding&#8221; is used.</p>
<p>To use JSONP with jQuery, just change the <strong>dataType</strong> parameter from <strong>json</strong> to <strong>jsonp</strong>. Now you do not have to worry about cross domain restriction or anything else. Just define a callback function (showTweets in this example) and you will receive the JSON response there.</p>
<pre class="brush:js">function getTweets(userName, count)
{
	$.getJSON(
		'http://api.twitter.com/1/statuses/user_timeline.json?screen_name=' + userName + '&amp;count='+ count +'&amp;callback=?',
		{},
		showTweets,
		'jsonp'
	);
}</pre>
<p>Now that we have the response, define <strong>showTweets</strong> function, iterate in the results and display them. Here is how.</p>
<pre class="brush:js">function showTweets(tweets)
{
	var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
	var str = '&lt;ul&gt;';
	str+= '&lt;h2&gt;Recent Tweets from ' + userName +'&lt;/h2&gt;';
	var i = 0;
	$.each(tweets, function(index,value)
	{
		if(i == count)	return;
		var dt = new Date(value.created_at);
		str+= '&lt;li&gt;&lt;p&gt;';
		str+= value.text;
		str+= '&lt;/p&gt;';
		str+='&lt;span&gt;';
		str+= dt.getDate() + '-' + months[dt.getMonth()] + '-' + dt.getFullYear();
		str+= ' at ' + dt.getHours() + ':' +dt.getMinutes();
		str+= ' From ' + value.source;
		str+= '&lt;/span&gt;&lt;/li&gt;';
		i++;
	});
	str+= '&lt;/ul&gt;';
	$('#tweets').html(str);
	$('#tweets &gt; ul &gt; li:odd').addClass('odd');
	$('#tweets &gt; ul &gt; li:even').addClass('even');
}</pre>
<p>I have used only some of the values from the JSON response. You can view the full response by typing the following into your browser.</p>
<pre class="brush:as3">http://api.twitter.com/1/statuses/user_timeline.json?screen_name=your_twitter_username&amp;count=10</pre>
<p>With this we are done. Have a look at the demo I have created.</p>
<p style="text-align: center;"><a href="http://vijayjoshi.org/examples/twitter-latest-tweets.html"><img class="size-full wp-image-453 aligncenter" title="View Demo" src="http://www.vijayjoshi.org/wp-content/uploads/demo.png" alt="View Demo" width="180" height="45" /></a></p>
<p>For full source code, just view page source. It&#8217;s all happening on your browser after all.</p>
<p>If you have any questions, let me know in comments.</p>
<div class="shr-publisher-886"></div><!-- Start Shareaholic LikeButtonSetBottom --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:right;height:30px;'><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.vijayjoshi.org%2F2010%2F12%2F24%2Fusing-twitter-api-and-jquery-to-display-recent-tweets%2F' data-shr_title='Using+Twitter+API+and+jQuery+to+display+recent+tweets'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom -->]]></content:encoded>
			<wfw:commentRss>http://www.vijayjoshi.org/2010/12/24/using-twitter-api-and-jquery-to-display-recent-tweets/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Working with XML Documents in PHP and jQuery</title>
		<link>http://www.vijayjoshi.org/2010/12/23/working-with-xml-documents-in-php-and-jquery/</link>
		<comments>http://www.vijayjoshi.org/2010/12/23/working-with-xml-documents-in-php-and-jquery/#comments</comments>
		<pubDate>Thu, 23 Dec 2010 10:17:15 +0000</pubDate>
		<dc:creator>Vijay Joshi</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Resources]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.vijayjoshi.org/?p=881</guid>
		<description><![CDATA[
			
				
			
		
Here is one more sample chapter from my book. It explains reading, writing and editing XML files with PHP and jQuery. Below are the chapter contents.
Loading XML from files and strings using SimpleXML
Accessing elements and attributes using SimpleXML
Searching elements using XPath
Reading an XML using DOM extension
Creating an XML using DOM extension
Modifying an XML using DOM extension
Parsing XML with jQuery

Loading XML ...]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop --><!-- End Shareaholic LikeButtonSetTop --><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.vijayjoshi.org%2F2010%2F12%2F23%2Fworking-with-xml-documents-in-php-and-jquery%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.vijayjoshi.org%2F2010%2F12%2F23%2Fworking-with-xml-documents-in-php-and-jquery%2F&amp;source=v08i&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Here is one more sample chapter from <a title="PHP jQuery Cookbook" href="http://www.vijayjoshi.org/2010/11/22/guys-i-am-writing-a-book/">my book</a>. It explains reading, writing and editing XML files with PHP and jQuery. Below are the chapter contents.</p>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Loading XML from files and strings using SimpleXML</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Accessing elements and attributes using SimpleXML</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Searching elements using XPath</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Reading an XML using DOM extension</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Creating an XML using DOM extension</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Modifying an XML using DOM extension</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Parsing XML with jQuery</div>
<ul>
<li>Loading XML from files and strings using SimpleXML</li>
<li>Accessing elements and attributes using SimpleXML</li>
<li>Searching elements using XPath</li>
<li>Reading an XML using DOM extension</li>
<li>Creating an XML using DOM extension</li>
<li>Modifying an XML using DOM extension</li>
<li>Parsing XML with jQuery</li>
</ul>
<p>To read the full chapter follow this link - <a title="Working with XML in PHP and jQuery" href="https://www.packtpub.com/article/working-with-xml-documents">https://www.packtpub.com/article/working-with-xml-documents</a></p>
<p>You can also read the chapter on <a title="JSON with PHP and jQuery" href="http://www.vijayjoshi.org/2010/12/20/working-with-json-in-php-and-jquery/">using JSON with PHP and jQuery</a>.</p>
<div class="shr-publisher-881"></div><!-- Start Shareaholic LikeButtonSetBottom --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:right;height:30px;'><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.vijayjoshi.org%2F2010%2F12%2F23%2Fworking-with-xml-documents-in-php-and-jquery%2F' data-shr_title='Working+with+XML+Documents+in+PHP+and+jQuery'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom -->]]></content:encoded>
			<wfw:commentRss>http://www.vijayjoshi.org/2010/12/23/working-with-xml-documents-in-php-and-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Working with JSON in PHP and jQuery</title>
		<link>http://www.vijayjoshi.org/2010/12/20/working-with-json-in-php-and-jquery/</link>
		<comments>http://www.vijayjoshi.org/2010/12/20/working-with-json-in-php-and-jquery/#comments</comments>
		<pubDate>Mon, 20 Dec 2010 11:56:18 +0000</pubDate>
		<dc:creator>Vijay Joshi</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Resources]]></category>

		<guid isPermaLink="false">http://www.vijayjoshi.org/?p=853</guid>
		<description><![CDATA[
			
				
			
		
I mentioned in a previous post that I was writing a book on PHP and jQuery. Well, after a labor of around 7 months the book is out now. It is available on Packt website as well as Amazon.
Meanwhile, to help you make a decision, a sample chapter from the book is available on Packt website. The chapter is Working ...]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop --><!-- End Shareaholic LikeButtonSetTop --><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.vijayjoshi.org%2F2010%2F12%2F20%2Fworking-with-json-in-php-and-jquery%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.vijayjoshi.org%2F2010%2F12%2F20%2Fworking-with-json-in-php-and-jquery%2F&amp;source=v08i&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>I mentioned in a <a title="PHP jQuery Cookbook" href="http://www.vijayjoshi.org/2010/11/22/guys-i-am-writing-a-book/">previous post</a> that I was writing a book on PHP and jQuery. Well, after a labor of around 7 months the book is out now. It is available on <a title="PHP jQuery Cookbook on Packt" href="http://goo.gl/WlFX">Packt website</a> as well as <a title="PHP jQuery Cookbook on Amazon" href="http://www.amazon.com/PHP-jQuery-Cookbook-Vijay-Joshi/dp/1849512744/">Amazon</a>.</p>
<p>Meanwhile, to help you make a decision, a sample chapter from the book is available on Packt website. The chapter is Working with JSON in PHP and jQuery and it contains 4 articles that explain creating, reading and writing JSON in PHP as well as jQuery.</p>
<p>Below are the topics that this chapter covers:</p>
<ul>
<li>Creating JSON in PHP</li>
<li>Reading JSON in PHP</li>
<li>Catching JSON parsing errors</li>
<li>Accessing data from a JSON in jQuery</li>
</ul>
<p>To read the full chapter, just follow this link <a title="Working with JSON in PHP jQuery" href="http://www.packtpub.com/article/working-json-php-jquery">http://www.packtpub.com/article/working-json-php-jquery</a></p>
<div class="shr-publisher-853"></div><!-- Start Shareaholic LikeButtonSetBottom --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:right;height:30px;'><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.vijayjoshi.org%2F2010%2F12%2F20%2Fworking-with-json-in-php-and-jquery%2F' data-shr_title='Working+with+JSON+in+PHP+and+jQuery'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom -->]]></content:encoded>
			<wfw:commentRss>http://www.vijayjoshi.org/2010/12/20/working-with-json-in-php-and-jquery/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>5 JSON resources for web developers</title>
		<link>http://www.vijayjoshi.org/2009/09/02/5-json-resources-for-web-developers/</link>
		<comments>http://www.vijayjoshi.org/2009/09/02/5-json-resources-for-web-developers/#comments</comments>
		<pubDate>Wed, 02 Sep 2009 13:06:05 +0000</pubDate>
		<dc:creator>Vijay Joshi</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[Resources]]></category>

		<guid isPermaLink="false">http://www.vijayjoshi.org/?p=366</guid>
		<description><![CDATA[
			
				
			
		
JSON(Javascript Object Notation) is defined as a lightweight data interchange format and fat-free lightweight alternative to xml. Since json is the native data form of Javascript, it can be used on the browser more easily then XML.
If you are a web developer who works with javascript/ajax a lot and still wondering what this json thing is, here is the first ...]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop --><!-- End Shareaholic LikeButtonSetTop --><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.vijayjoshi.org%2F2009%2F09%2F02%2F5-json-resources-for-web-developers%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.vijayjoshi.org%2F2009%2F09%2F02%2F5-json-resources-for-web-developers%2F&amp;source=v08i&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>JSON(Javascript Object Notation) is defined as a lightweight data interchange format and fat-free lightweight alternative to xml. Since json is the native data form of Javascript, it can be used on the browser more easily then XML.</p>
<p>If you are a web developer who works with javascript/ajax a lot and still wondering what this json thing is, here is the first step to start with. Read <a title="Definition of JSON and its uses" href="http://www.vijayjoshi.org/2008/08/20/what-is-json-definition-and-uses-in-javascript/" target="_self">this post about json</a> and its uses.</p>
<p>Below is a list of 5 resources from where you can learn about json in detail.</p>
<h2><a title="json.org" href="http://json.org/" target="_self">1- json.org</a></h2>
<p>First place to go to find definitive information. Douglas crockford is the man who invented json. json.org is a one stop place for everything json.</p>
<h2><a title="Mastering JSON" href="http://www.hunlock.com/blogs/Mastering_json_%28_JavaScript_Object_Notation_%29" target="_self">2- Mastering JSON</a></h2>
<p>Detailed examples as well as retrieving and sending json data between browser and server. You cam also find tips on security and best practices.</p>
<h2><a title="JSON vs XML Debate" href="http://ajaxian.com/archives/json-vs-xml-the-debate" target="_self">3- JSON vs XML debate</a></h2>
<p>Arguments are going on between which is better to use as a data format. Go read and judge yourself.</p>
<h2>4- JSON with Ajax and PHP[video]</h2>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="560" height="340" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="src" value="http://www.youtube.com/v/BEZvnGSk6A4" /><embed type="application/x-shockwave-flash" width="560" height="340" src="http://www.youtube.com/v/BEZvnGSk6A4"></embed></object></p>
<h2>5- Slide on JSON</h2>
<p><object style="margin:0px" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="425" height="355" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowScriptAccess" value="always" /><param name="src" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=javascriptobjectnotationjson-090330015205-phpapp01&amp;stripped_title=java-script-object-notation-json-1219489" /><param name="allowfullscreen" value="true" /><embed style="margin:0px" type="application/x-shockwave-flash" width="425" height="355" src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=javascriptobjectnotationjson-090330015205-phpapp01&amp;stripped_title=java-script-object-notation-json-1219489" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<div class="shr-publisher-366"></div><!-- Start Shareaholic LikeButtonSetBottom --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:right;height:30px;'><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.vijayjoshi.org%2F2009%2F09%2F02%2F5-json-resources-for-web-developers%2F' data-shr_title='5+JSON+resources+for+web+developers'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom -->]]></content:encoded>
			<wfw:commentRss>http://www.vijayjoshi.org/2009/09/02/5-json-resources-for-web-developers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What is JSON? Definition and uses in JavaScript</title>
		<link>http://www.vijayjoshi.org/2008/08/20/what-is-json-definition-and-uses-in-javascript/</link>
		<comments>http://www.vijayjoshi.org/2008/08/20/what-is-json-definition-and-uses-in-javascript/#comments</comments>
		<pubDate>Wed, 20 Aug 2008 09:05:43 +0000</pubDate>
		<dc:creator>Vijay Joshi</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[JSON]]></category>

		<guid isPermaLink="false">http://www.vijayjoshi.org/?p=67</guid>
		<description><![CDATA[
			
				
			
		
Recently JSON has become a very popular data interchange format with more and more developers and a large number of big players in the web arena opting for it over XML.  In this article we will explore what json is all about and how we can use it. We will proceed in the following manner:


Definition, Characteristics and Structure of json


Example


Parsing ...]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop --><!-- End Shareaholic LikeButtonSetTop --><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.vijayjoshi.org%2F2008%2F08%2F20%2Fwhat-is-json-definition-and-uses-in-javascript%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.vijayjoshi.org%2F2008%2F08%2F20%2Fwhat-is-json-definition-and-uses-in-javascript%2F&amp;source=v08i&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Recently <a title="JSON" href="http://json.org/" target="_blank">JSON</a> has become a very popular data interchange format with more and more developers and a large number of big players in the web arena opting for it over XML.  In this article we will explore what json is all about and how we can use it. We will proceed in the following manner:</p>
<ul>
<li>
<h4 style="text-align: justify;"><span style="color: #17365d;">Definition, Characteristics and Structure of json</span></h4>
</li>
<li>
<h4><span style="color: #17365d;">Example</span></h4>
</li>
<li>
<h4><span style="color: #17365d;">Parsing json text</span></h4>
</li>
<li>
<h4><span style="color: #17365d;">Reading values from json</span></h4>
</li>
</ul>
<p>Let’s go through each of these one by one.</p>
<h2><span style="color: #17365d;">Definition, Characteristics and Structure of json</span></h2>
<p>JSON which stands for JavaScript Object Notation can be defined as a lightweight data interchange format. It is also said a fat-free lightweight alternative to xml. It is a text format which is programming language independent and is native data form of JavaScript. It is lighter and faster than xml. The credit to make json popular goes to <a title="Douglas Crockford" href="http://crockford.com/" target="_blank">Douglas Crockford.</a></p>
<p>Since JSON is the native data form of JavaScript, it can be used on the client side in an Ajax application more easily then XML. Implementations of JSON has been done in most of the languages (<a title="JSON implementations" href="http://json.org/">check here</a>) we will stick to javascript as of now.</p>
<p>A JSON object starts with { and ends with }.  In between you can have key value pairs which are enclosed with double quotes (“) and separated by a colon (:).</p>
<p>Keys are simple strings and values can be either array, string, number or Boolean. Values enclosed between { and } are objects and those between [ and ] are arrays.</p>
<h2><span><span style="color: #17365d;">Example</span></span></h2>
<p>A JSON can be as simple as</p>
<pre lang="javascript">{
“name”:”name1”, “age”:”25”, ”address”: "anywhere"
}</pre>
<p><span style="text-decoration: underline;">Example 1</span><br />
An example using arrays</p>
<pre lang="javascript">{
"name": "name1", "age":"25", "phoneNumbers": ["9999123456", "9999678900","1234567890" ]
}</pre>
<p><span style="text-decoration: underline;">Example 2</span><br />
A more complex example which has values in objects and objects are array elements.</p>
<pre lang="javascript">{
"results":
 {
   "students":
   [
     {
      "id" : "1",
      "name": "Ramu"
     },
     {
      "id": "2",
      "name": "Shyamu"
     },
     {
      "id" : "3",
      "name": "Damu"
     }
   ]
 }
}</pre>
<p><span style="text-decoration: underline;">Example 3</span></p>
<p>Those familiar with JavaScript will see that syntax is same we use while declaring objects in JavaScript.In the above example we have a json where results is the key and its value is an object.</p>
<p><span id="more-67"></span><br />
The value for results again contains an object which has the value students. The value of students is an array ( note the [ ). This array contains 3 values which are again objects. The first one is</p>
<pre lang="javascript">{
      "id" : "1",
      "name": "Ramu",
}</pre>
<p>This object has 2 keys, id and name. The value for key id is 1 and the value for key name is Ramu.</p>
<h2><span><span style="color: #17365d;">Parsing json text</span></span></h2>
<p>Now suppose you received a json string as responseText in an Ajax application. You will now want to read the values that this structure has brought with it and use them in your application. But wait  - there is one more step in between and that is parsing of this text.</p>
<h5>Why do I have to parse it ??</h5>
<p>Since JSON is a text format, we will have to parse it to a format that JavaScript can recognize and read upon. We can use <a title="Javascript eval" href="http://www.w3schools.com/jsref/jsref_eval.asp">eval</a> to parse it.</p>
<pre lang="javascript">var myJsonObject = eval( ' ( ' + JsonString + ' ) ' );</pre>
<p>This will convert the string JsonString to a javascript object. Now you have an object available in javascript. You are free to do whatever with its values.</p>
<p>One word of caution. Use of eval is not recommended since eval is considered evil. This is because eval can execute any javascript code that is passed to it. So better use a JSON parser. It will make sure that the json string passed to it is thoroughly checked and scripts are rejected. I personally use<a title="Json parser" href="http://json.org/json.js" target="_blank"> json.js</a> ( now json2) by Douglas Crockford.</p>
<pre lang="javascript">var myJsonObject = JsonString.parseJSON();</pre>
<h2><span><span style="color: #17365d;">Reading values from json</span></span></h2>
<p>After a json string is successfully parsed, you can use it like a normal javascript variable. Here we will try to read values from the structure in example 3. This structure holds the records of 3 students.</p>
<p>Suppose we have the parsed object in variable studentRecords.</p>
<p>studentRecords.results will give an object. studentRecords.results.students will give the array that has 3 records. Now we can iterate over this array and get values.</p>
<pre lang="javascript">var rec = studentRecords.results.students;
for(var i=0; i&lt;rec.length; i++)
{
   var aStudent = rec[i];
   alert(aStudent.id);
   alert(aStudent.name);
}</pre>
<p>With this we come to the end of this article. If you have any queries or suggestions, do tell me.</p>
<p>You can see a complete working example with the json string of example 3 <a title="Json Example" href="http://www.vijayjoshi.org/examples/json/json_basic_example.html">here</a>.</p>
<p>--</p>
<div class="shr-publisher-67"></div><!-- Start Shareaholic LikeButtonSetBottom --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:right;height:30px;'><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.vijayjoshi.org%2F2008%2F08%2F20%2Fwhat-is-json-definition-and-uses-in-javascript%2F' data-shr_title='What+is+JSON%3F+Definition+and+uses+in+JavaScript'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom -->]]></content:encoded>
			<wfw:commentRss>http://www.vijayjoshi.org/2008/08/20/what-is-json-definition-and-uses-in-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

