<?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/tag/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 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>Guys, I am writing a book</title>
		<link>http://www.vijayjoshi.org/2010/11/22/guys-i-am-writing-a-book/</link>
		<comments>http://www.vijayjoshi.org/2010/11/22/guys-i-am-writing-a-book/#comments</comments>
		<pubDate>Mon, 22 Nov 2010 11:25:08 +0000</pubDate>
		<dc:creator>Vijay Joshi</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[Resources]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.vijayjoshi.org/?p=671</guid>
		<description><![CDATA[
			
				
			
		

Yes that&#8217;s right and it is called PHP jQuery Cookbook.  As the name suggests, it&#8217;s about creating rich internet applications using 2 of my favorite technologies &#8211; PHP and jQuery.
I love coding web apps and helping others find solutions. Perhaps that is the reason I agreed to write when the wonderful people at Packt Publishing contacted me. (Also, a book ...]]></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%2F11%2F22%2Fguys-i-am-writing-a-book%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.vijayjoshi.org%2F2010%2F11%2F22%2Fguys-i-am-writing-a-book%2F&amp;source=v08i&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-630" title="PHP jQuery Cookbook" src="http://www.vijayjoshi.org/wp-content/uploads/2749OS_MockupCover_Cookbook_0.jpg" alt="PHP jQuery Cookbook" width="320" height="395" /></p>
<p>Yes that&#8217;s right and it is called <strong><a title="PHP jQuery Cookbook" href="https://www.packtpub.com/php-jquery-cookbook-to-create-interactive-web-applications/book">PHP jQuery Cookbook</a></strong>.  As the name suggests, it&#8217;s about creating rich internet applications using 2 of my favorite technologies &#8211; PHP and jQuery.</p>
<p>I love coding web apps and helping others find solutions. Perhaps that is the reason I agreed to write when the wonderful people at <a title="Packt Publishing" href="http://www.packtpub.com/">Packt Publishing</a> contacted me. (Also, a book on resume isn&#8217;t that bad either <img src='http://www.vijayjoshi.org/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> )</p>
<p>Another reason is I love Open source. It is a lovely philosophy and I am a strong believer in it. Having learnt so much from the Open source community since I started coding, I have always wanted to return something back. Nothing could have been better then this. And did I mention, Packt pays <a title="Packt Open Source Royalties" href="http://authors.packtpub.com/content/open-source-royalties">direct royalty to a project</a> if they sell a book written on the same.</p>
<p><strong><span id="more-671"></span></strong></p>
<p>Long story short, the book is cookbook with around 65 recipes covering PHP, jQuery, MySql and some API&#8217;s (Flickr, Youtube).</p>
<p>Structure wise, it has 10 chapters in all.</p>
<ul>
<li>Handling Events</li>
<li>Combining PHP and jQuery</li>
<li>Working with XML documents</li>
<li>Working with JSON</li>
<li>Working with Forms</li>
<li>Adding Visual effects to forms</li>
<li>Creating cool Navigation Menus</li>
<li>Data binding with PHP and jQuery</li>
<li>Enhancing your site with PHP and jQuery</li>
<li>Appendix &#8211; FireBug</li>
</ul>
<p>The book is available now <span style="text-decoration: line-through;">will be available by 14th December</span> <del datetime="2010-12-01T06:28:41+00:00">Christmas this year or January next year</del>. You can have a look in detail about what it is all about on Packt Website. Order a copy in case you find it interesting <img src='http://www.vijayjoshi.org/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<div class="shr-publisher-671"></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%2F11%2F22%2Fguys-i-am-writing-a-book%2F' data-shr_title='Guys%2C+I+am+writing+a+book'></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/11/22/guys-i-am-writing-a-book/feed/</wfw:commentRss>
		<slash:comments>6</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>

